[MDEV-8548] using LIMIT option makes query slower Created: 2015-07-27 Updated: 2022-09-12 Resolved: 2022-09-12 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | OTHER |
| Affects Version/s: | 10.0.17, 10.0.21 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | aftab khan | Assignee: | Sergei Petrunia |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Attachments: |
|
| Description |
|
Query below returns 1000 rows in sorted order:
It takes 1-3 minutes to return result set, however if we do not use ORDER BY then of course its very faster. Similarly if we do not use 'LIMIT N' option then query start to return all rows quickly but we require only N number of rows:
As work around we are using sub-query to help fix this performance issue:
Why does LIMIT option make it slower? |
| Comments |
| Comment by aftab khan [ 2015-07-27 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Another issue is that both versions of query return different result: – Original Query - using LIMIT option
– Modified Query - using sub-query and outer query using LIMIT
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-07-28 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Regarding the question in the comment about different results, see https://mariadb.com/kb/en/mariadb/why-is-order-by-in-a-from-subquery-ignored/ Regarding the original issue with performance, please provide
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by aftab khan [ 2015-07-29 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Elena, >Regarding the question in the comment about different results, see https://mariadb.com/kb/en/mariadb/why-is-order-by-in-a-from-subquery-ignored/ >EXPLAIN for the query with ORDER BY ... LIMIT;
>EXPLAIN for the query with ORDER BY but without LIMIT; It uses correct index:
However we can use INDEX HINT for the query with ORDER BY .. LIMIT to ensure correct execution plan:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-01 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
aftab.khan, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by aftab khan [ 2015-08-13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Elena, I have attached following file that has both SHOW CREATE + SHOW VARIABLES: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-20 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I cannot reproduce the problem so far. I mean, I can easily get the described plans, but I don't see the plan with index/PRIMARY key perform any worse than the plan with ref/ID_RESSOURCE_STATUT_DATE_PASSAGE. On the contrary, actually, see below. Result sets are redirected to /dev/null, it is only plans and execution times that we are interested in.
Of course, it might be caused by the artificially generated data, but at the very least it shows that the plan with ref/ID_RESSOURCE_STATUT_DATE_PASSAGE is not always beneficial. Could you please provide the similar output – execution time for the query with LIMIT, without LIMIT, and with the LIMIT and index hint? Please also provide the output of show index in PROD_RESSOURCES_NAS, it will help me to generate data better resembling yours. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by aftab khan [ 2015-08-20 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
here is the output of the queries as requested:
I ran the first query again just to clarify that it is slow when not using index hints
I would also like to share the distribution of rows by ID_RESOURCE_STATUS
Explain output of the query not using INDEX HINTS looks more optimal but query takes longer to complete
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-20 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I crafted the data where I can reproduce the performance problem. Here is how it looks in my case: Data
Results for server with all defaults (importantly, without the performance schema)
As you can see, for 0 it works as expected. Results for server with performance schema enabledI put them separately, because even though the ratio holds, the values are essentially different.
ANALYZE does not help:
Engine-independent statistics also does not help:
I uploaded the data dump here: ftp://ftp.askmonty.org/public/mdev8548_data.dump.gz | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by aftab khan [ 2015-08-20 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Elena, Looks like we also have ID_RESOURCE_ID=1 at the very end of the data ordered by the primary key. PK value boundries:
ID_RESSOURCE_STATUT=1 is at the very end of the table
ID_RESSOURCE_STATUT=0 is distributed across the table
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by aftab khan [ 2015-08-21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi, I think we have not provided query times without INDEX HINT, the results are interesting: Execution time for ID_RESSOURCE_STATUT=0
Execution time for ID_RESSOURCE_STATUT=1
Observations: 1. Total row count is 7459826 where ID_RESSOURCE_STATUT=0 but data is distributed across the table, query execution time is 0.03 sec | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2022-09-12 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
10.0 was EOLed in March 2019 |