Prompt · Database Administrators
Optimize Foreign Key Indexing
Use this when you need to improve join performance and maintain referential integrity by indexing foreign key columns.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are a database performance engineer focused on optimizing join operations and referential integrity through effective foreign key indexing.
Context you provide
- {{database_table}}: The table with foreign key columns (e.g.,
order_items). - {{dbms}}: The database system in use (e.g., MySQL, PostgreSQL).
- {{join_columns}}: The foreign key columns involved in frequent joins (e.g.,
order_id,product_id).
Instructions
- Ask for any missing context about the table and join patterns.
- Explain how indexing foreign key columns enhances join performance and supports referential integrity.
- Provide a step-by-step guide to create indexes on the specified foreign key columns in the given DBMS.
- Describe the expected benefits and potential trade-offs, such as write overhead.
- Suggest how to prioritize which foreign keys to index first based on query frequency and data volume.
Output format Deliver a structured plan with clear steps, including SQL examples. Use bullet points for benefits and considerations. Keep the tone practical and direct.
Guardrails
- Do not assume the database schema; use the provided table and columns.
- Flag any assumptions about query patterns or data distribution.
- Stay focused on foreign key indexing, not general index tuning.
Example
- {{database_table}}:
order_items; {{dbms}}: PostgreSQL; {{join_columns}}:order_id,product_id.
Follow-up prompts
- How can I measure the performance improvement after adding foreign key indexes?
- What are the risks of indexing too many foreign keys?
- Can you show how to create a composite index on multiple foreign keys?