Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 05-31-2007, 06:33 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default When to create indexes on custom tables?

I have a few sites that use a lot of custom pages, scripts, and data. There's lots of new tables added. Obviously each table has a primary key (usually the id column) but how do i know when I should create an index on other columns? I have also seen (looking at vbulletin's default tables) that some indexes are for multiple columns. What's the difference? Are there any guidelines to follow for determining when to create a new index and what type (how many columns to include) to create?

Reply With Quote
  #2  
Old 05-31-2007, 10:18 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Antivirus View Post
I have a few sites that use a lot of custom pages, scripts, and data. There's lots of new tables added. Obviously each table has a primary key (usually the id column) but how do i know when I should create an index on other columns? I have also seen (looking at vbulletin's default tables) that some indexes are for multiple columns. What's the difference? Are there any guidelines to follow for determining when to create a new index and what type (how many columns to include) to create?

Any columns used in a where, join or sort or having or just about any clause should be indexed. Indexing on multiple columns can save space but requires all columns in the index be used.

The single best way to determine which should be indexed is to profile your sql with explain, which is easily done by just putting the keyword expain in front of select.

For example if you have the query

select f1 from table1 join table 2 using (f2) where f2 = 'foo' and f3 > 40

then run

explain select f1 from table1 join table 2 using (f2) where f2 = 'foo' and f3 > 40

What they output of explain does is best looked up in the mysql manual, but basically you want every row to be using an index and you rarely want an all table scan.
Reply With Quote
  #3  
Old 05-31-2007, 11:12 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks, that's useful info i will see how it works out. I suyppose if i am already running the query in vbulletin, if debug mode is enables, i can just click on the (explain) thingie and will give me same info, correct?
Reply With Quote
  #4  
Old 06-01-2007, 12:29 AM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Using EXPLAIN really helps.

But a really simple thing to remember is if you are using a column primarily in joins / where clauses, such as a 'userid' column in another table, then it needs one. Well, doesn't need - but it will speed up the query significantly. Just like in a book, finding something using an index is FAR faster than reading every page looking for that word.

Code:
SELECT * FROM jokes WHERE userid = 1;
Code:
SELECT *
FROM jokes 
LEFT JOIN users on (jokes.userid = users.userid);
Both of them are essentially using the 'userid' column to narrow down the results, so it is much faster if it can do so using the index (and not actually go into the data, which is slower).

As eik... said, the same thing goes for sorting. If you are often sorting by title (select * from jokes order by title asc) then you may want to consider indexing it. This is assuming the table has several other columns. The larger an index becomes, the less useful it is (relative to actual row size).

If you have some time, watch this MySQL tuning vid,
http://video.google.com/videoplay?do...24540025172110

I remember it being quite helpful when I watched it last spring.

Edit,

Yes clicking on the Explain link will show you the EXPLAIN info for each query.
Reply With Quote
  #5  
Old 06-01-2007, 10:13 AM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks - very interesting video. A bit long (40 mins) but really starts getting interesting and focusing on indexing about 9-10 minutes in. Learned a lot from it, gonna watch it a few more times to allow some stuff to sink in better as well.
Reply With Quote
  #6  
Old 06-01-2007, 02:34 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No problem.

I really wish they would post more stuff like that... especially for PHP/MySQL.
Reply With Quote
  #7  
Old 06-02-2007, 03:43 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So for instance, i have the query shown in query.gif (attached) which I am trying to optimize. I ran explain on it (also attached as explain.gif) and it's telling me that I need to create an index on table 1`c` correct?

I created an index on table scst_fb for fbid and when i run the query, it's still saying filesort in the extra column for explain. Shouldn't it be using the index now?
Attached Images
File Type: gif query.gif (28.1 KB, 0 views)
File Type: gif explain.gif (35.7 KB, 0 views)
Reply With Quote
  #8  
Old 06-03-2007, 10:49 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

the file sort means that the system has to do an extra pass on the table to retrieve the rows in sorted order. Nested queries are tricky, you can get some optimisation by setting up a query cache, but if it were me I would use mysql v 5+ and set up a view.

First run the nested query in explain to make sure it is properly indexed.
Reply With Quote
  #9  
Old 06-04-2007, 03:01 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I see. For anyone else interested, here's a direct link to the Mysql manual explaining the use of the explain command:

http://dev.mysql.com/doc/refman/4.1/en/explain.html

I'll be reading it today, i am sure it will help explain things - thanks.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:40 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07228 seconds
  • Memory Usage 2,265KB
  • Queries Executed 14 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (2)postbit_attachment
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • postbit_attachment
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete