| |
Welcome to the dBforums forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact contact support.
If you prefer not to see double-underlined words and corresponding ads, place your cursor here for ContentLink opt out.
|
 |

10-25-07, 15:04
|
|
Registered User
|
|
Join Date: May 2004
Posts: 69
|
|
|
execution time of a query with & without ORDER BY
|
hi
recently i wonder why "ORDER BY" increase execution time of a query?
i run these two query over a indexed field (flag1):
Code:
SELECT * FROM tbl1 WHERE 1 =1 AND flag1=1 LIMIT 10(Query took 0.2401 sec)
SELECT * FROM tbl1 WHERE 1 =1 AND flag1=1 ORDER BY flag1 LIMIT 10(Query took 2.1315 sec)
u see? flag1 exist in both WHERE & ORDER BY section, but second query took 10 times more than first query. any idea about optimizin second query or table structure for decrease execution time?
|
Last edited by bono56 : 10-25-07 at 15:09.
|

10-25-07, 15:18
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
|
|
here is my rule of optimization: if you need the results in a particular order, use ORDER BY -- otherwise, don't
simple, innit 
|
|

10-25-07, 17:47
|
|
Registered User
|
|
Join Date: May 2004
Posts: 69
|
|
|
i need it, but for me it was odd, this fld has index with Cardinality=1.
|
|

10-26-07, 04:20
|
|
Registered User
|
|
Join Date: Aug 2004
Location: France
Posts: 754
|
|
Quote:
|
Originally Posted by bono56
i need it, but for me it was odd, this fld has index with Cardinality=1.
|
Do you mean that "flag1=1" returns only 1 row ?
You agree that in your example you don't need the "ORDER BY flag1" clause since you only select the rows with "flag1=1". It was just a test to demonstrate how the MySQL optimizer is dumb sometimes, wasn't it ?
Just for fun, Oracle seems more intelligent :
Code:
rbaraer@Ora10g> select * from transactions where codevaleur = 'AAABA';
11951 rows selected.
Elapsed: 00:00:00.95
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=25 Card=11039 Bytes=408443)
1 0 INDEX (RANGE SCAN) OF 'CP_TRANSACTIONS' (INDEX (UNIQUE)) (Cost=85 Card=11039 Bytes=408443)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
888 consistent gets
0 physical reads
0 redo size
641474 bytes sent via SQL*Net to client
9420 bytes received via SQL*Net from client
798 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
11951 rows processed
rbaraer@Ora10g> select * from transactions where codevaleur = 'AAABA' order by codevaleur;
11951 rows selected.
Elapsed: 00:00:00.95
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=25 Card=11039 Bytes=408443)
1 0 INDEX (RANGE SCAN) OF 'CP_TRANSACTIONS' (INDEX (UNIQUE)) (Cost=85 Card=11039 Bytes=408443)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
888 consistent gets
0 physical reads
0 redo size
641474 bytes sent via SQL*Net to client
9420 bytes received via SQL*Net from client
798 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
11951 rows processed
rbaraer@Ora10g>
Regards,
rbaraer
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready  .
|
|

10-26-07, 04:22
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
|
|
the key word being "seems"

|
|

10-26-07, 04:35
|
|
Registered User
|
|
Join Date: Aug 2004
Location: France
Posts: 754
|
|
Quote:
|
Originally Posted by r937
the key word being "seems"

|
In this particular case, since the execution times are the same with Oracle and the "order by" takes 9x more time with MySQL, I would even risk a "is more intelligent", wouldn't you ?
But I know you are going to say :
Quote:
|
the key word being "In this particular case"
|
so I won't go any further
I have a lot of experience with Oracle, but quite few with MySQL and I'm interested in knowing more about it. From your experience, would you say that the MySQL optimizer generally works well ?
rbaraer
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready  .
|
|

10-26-07, 07:06
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
|
|
yes i would 
|
|

10-29-07, 10:40
|
|
SQL Apprentice
|
|
Join Date: Jan 2007
Location: hiding
Posts: 8,144
|
|
This public service announcement was brought to you by.... 
__________________
George
You only stop learning when you stop asking questions.
|
|

11-02-07, 10:48
|
|
Registered User
|
|
Join Date: May 2004
Posts: 69
|
|
another point that was strange for me:
look at these queries executed on a table with 700,000 record:
Code:
SELECT * FROM tbl1 WHERE flag2='X' ORDER BY fld1 (return 50,000 total)
SELECT * FROM tbl1 WHERE flag2='Y' ORDER BY fld1 (return 5,000 total)
despite of number of records return with the where criteria, execution time is the same, why?
i supposed mysql first select records have that criteria, then go to sort them, so execution time must depends on number of records?
|
|

11-06-07, 11:36
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
|
|
Perhaps because you have an index on the flag2 field?
|
|

11-07-07, 12:16
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 35
|
|
re
aschk is right, putting an index on that field might speed things up. i have a similar problem:
Code:
( SELECT SQL_CALC_FOUND_ROWS [..fields here..] FROM `table_tmp` INNER JOIN `cat` ON `cat`.`cat_id` = `table_tmp`.`cat_id` WHERE `flag` = '1' AND `price1` > 0 AND `price2` > 0 AND `price3` > 0 AND `price4` > 0 AND `table_tmp`.`cat_id` IN (1,2,3,4) ) LIMIT 0,25
this query runs in 1 second. when i add ORDER BY `flag` DESC, `price1` ASC it runs in 3.2 seconds. i have indexes on `flag` and on `price`. i've added an index on `flag` + `price1` and the query runs now in 2.05 sec. how can i improve it better? thanks
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|