Complete AI Training

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.

All 19 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. 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

  1. Ask for any missing context about the table and join patterns.
  2. Explain how indexing foreign key columns enhances join performance and supports referential integrity.
  3. Provide a step-by-step guide to create indexes on the specified foreign key columns in the given DBMS.
  4. Describe the expected benefits and potential trade-offs, such as write overhead.
  5. 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?