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
Query Optimization Direction
Old 08-29-2008, 03:13 PM Query Optimization Direction
Experienced Talker

Posts: 38
Trades: 0
Hi, I have a 6000-row table for "products". Let's say I need to select the latest 30 rows to display on the website, the simple query would look like this right?

Code:
SELECT * FROM  products ORDER BY id DESC LIMIT 30
In phpMyAdmin, it shows that query took 0.0039 sec. Though, if I do an "EXPLAIN", it shows that it is scanning through all 6000 rows in the table:

Code:
id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | Extra
1  | SIMPLE      | products | index | NULL          | PRIMARY | 4       | NULL | 6908 |
For the sake of server memory concern (I'm getting numerous warnings from hosting company regarding memory issues), I've try to find a better way to do queries, and I'm not sure if I am heading the right direction or not, I came across creating the follow:

Code:
SELECT * FROM products
WHERE id > ((SELECT MAX(id) AS FROM products) - 30)
ORDER BY id DESC
It does the exact SAME thing as the short query above, except the query is reference off an indexed column. Though, in phpMyAdmin, it took 0.0047 sec to complete. Following is the "EXPLAIN":

Code:
id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | Extra
1  | PRIMARY     | products | range | PRIMARY       | PRIMARY | 4       | NULL | 75   | Using where
2  | SUBQUERY    | NULL     | NULL  | NULL          | NULL    | NULL    | NULL | NULL | Select tables optimized away
As you can see, the query only scanned 75 rows instead of 6000 rows... but why is it taking longer to complete, is it because of the nested query?

Or the most important question, am I even heading the right direction?
__________________
-Thomas Yeung

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
tomazws is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-29-2008, 04:50 PM Re: Query Optimization Direction
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
First thing would be to get rid of the "SELECT *" and use fieldlists

http://www.webmaster-talk.com/the-da...tml#post766485

secondly I'd be looking to upgrade the hosts to a better one
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-01-2008, 03:51 PM Re: Query Optimization Direction
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
6,000 rows is nothing. As you can see from the execution time.

Traversing an index has a cost associated with it. SQL will prefer a table scan for a very small table, which you might be seeing, although the break even point should be much smaller. As Chris says, select * isn't helping, but seems the same in both queries.

Probably, depending on how MySQL was written, the fact that you use ID in a predicate (where clause vs order by + limit) must trick the optimizer.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 09-01-2008, 10:18 PM Re: Query Optimization Direction
Junior Talker

Posts: 4
Name: Jeff
Trades: 0
Everybody has valid points. However, no one mentioned that your ORDER BY clause is to blame for scanning all 6000 table rows. Without a predicate that is supported by an index within the where clause, all rows must first be scanned, then sorted. One trick which I've used when just a few of the columns are needed is to create a composite index which contains all the columns needed to satisfy the query requirements. This technique allows the query to be completed using only the index without having to "join" to the physical table to extract the addition columns not in the query. There are also some Oracle-specific techniques, but that's apples & oranges...good luck!
ImaCubFan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Query Optimization Direction
 

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.14021 seconds with 12 queries