Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:12:35+00:00 2026-05-18T02:12:35+00:00

I’m a relative newbie when it comes to database design. I know how to

  • 0

I’m a relative newbie when it comes to database design. I know how to define the tables, but I’m starting to run into performance issues as my database grows. Can someone give me some guidance on how to use mysql indexes to improve query performance? My understanding of indexes is that they prevent the entire database table from being searched line by line, while still being able to return valid results.

I have typically just defined a primary key for each table that gives a unique id for each row. Is the idea behind defining an “index” that you do it for each field that will be used in the WHERE clause of a mysql statement? Not sure if that is too general of a statement.

As an example, lets say we have the following three tables:

products

products_id, products_name

categories

categories_id, categories_name

products_to_categories

products_id
categories_id

and we want to run the following query:

SELECT p.products_name, c.categories_name 
  FROM categories as c 
  JOIN products as p 
  JOIN products_to_categories as p2c 
 WHERE p.products_id=12345

Would we define both fields in the products_to_categories table as indexes and then the products_id and categories_id as primary keys within their parent table?

Any advice or guidance on the general approach to indexes would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:12:35+00:00Added an answer on May 18, 2026 at 2:12 am

    The best way to think about indexes is how you expect to query the data.

    Let’s assume that products_id and categories_id are PRIMARY KEY in your database, which means they are indexed automatically. If not, start with that.

    When I do multi-join tables, if you want to be paranoid, create two indexes to allow bi-directional accessibility of the IDs, e.g.

    CREATE TABLE products_to_categories (
        products_id integer unsigned NOT NULL,
        categories_id integer unsigned NOT NULL,
        INDEX p_to_c (products_id,categories_id),
        INDEX c_to_p (categories_id,products_id)
    ) ENGINE=MyISAM;
    

    This takes a lot of space, but it will be really, really fast, and unless you query both directions (from products to categories, and then reverse), it’s probably overkill. Alternatively, by default, I do:

    CREATE TABLE products_to_categories (
        products_id integer unsigned NOT NULL,
        categories_id integer unsigned NOT NULL,
        INDEX p (products_id),
        INDEX c (categories_id)
    ) ENGINE=MyISAM;
    

    If you need some sort of constraint (many-to-one, one-to-many) then change your index types to UNIQUE etc.

    In general, start with the latter definition, do your query, and run an EXPLAIN on it. If it shows anything which is more than 1 for the number of matched rows (except for the first table), then re-work the indexes.

    Database indexing is really more a matter of testing and diagnostics than many think. I didn’t know how to do this for a while, until I actually had a problem. In short:

    1. Create your indexes
    2. Determine your queries
    3. Run EXPLAIN on your queries, and run timing tests to determine query speed!
    4. Adjust your indexes
    5. Go back to 3

    As one user commented below, EXPLAIN is a good starting point before running timing tests, but nothing beats actual timing tests in the wild.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.