Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 10-12-2007, 07:46 PM Slow SELECT queries
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
I am need of some MYSQL optimization advice. I have a table that contains 250,000+ plus rows and when I try to select one at random I get execution times of 60+ seconds.

Here is the SQL I am using:

SELECT DISTINCT * FROM table WHERE col_a = 0123456789 AND col_b = "TYPE"

Both col_a and col_b are indexes, neither are primary because there exists duplicate values for each column.

I need some help to get this number down to a more pratical number.
__________________
~Mark Romero
-Co Founder | Tech Guru,
Please login or register to view this content. Registration is FREE

-FireFox Advocate,
Please login or register to view this content. Registration is FREE
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
 
Register now for full access!
Old 10-13-2007, 12:56 AM Re: Slow SELECT queries
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Distinct is very expensive. If you can specify the columns you want instead of using * that will help to some degree. Also consider a compound index with both of the columns in your where clause.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 10-13-2007, 02:13 AM Re: Slow SELECT queries
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
By removing DISTINCT it decreases by about 5-10 seconds, I have not yet tried specifying the columns. Also I am not familiar with compound indexes, I will have research those.
__________________
~Mark Romero
-Co Founder | Tech Guru,
Please login or register to view this content. Registration is FREE

-FireFox Advocate,
Please login or register to view this content. Registration is FREE
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
Old 10-13-2007, 04:51 AM Re: Slow SELECT queries
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Take a look at the execution plan with and without the distinct operator...

A compound index is one that has more than one column. If you put the two of the columns in your where clause in an index - in the same order they're used in the query - the server won't have to read the table rows to find the records that meet your criteria, only to gather them. It's probably using the first column/index and then evaluating all of the matches against the rest of your where clause. You can cut out all of the work from "false positives" this way.

Also higher cardinality - mostly unique entries - in an index makes it more efficient.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 10-14-2007, 11:58 AM Re: Slow SELECT queries
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
Thanks Forrest, that worked great.
__________________
~Mark Romero
-Co Founder | Tech Guru,
Please login or register to view this content. Registration is FREE

-FireFox Advocate,
Please login or register to view this content. Registration is FREE
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
Reply     « Reply to Slow SELECT queries
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14945 seconds with 12 queries