

Then, it sorts the sorted result set by the Milliseconds column in descending order. Without the ordering it can probably just return after fetching the first 50 rows while the ordering has to repartition all data in order to complete the query. The ORDER BY clause in SQL will help us to sort the records based on the specific column of a table. SQLite sorts rows by AlbumId column in ascending order first. The execution time might not be affected by the ordering at all but instead by the repartition. Without the ordering part the repartition only takes ~5 ms. IndexRangeScan laravel.site_keyword, PRIMARY KEY (site_id, keyword_id) scan: est_table_rows:7,790,339 est_filtered:346,316įrom another look at the profile it seems like the repartition (~120 ms) is the bottleneck when ordering with the joined table. Repartition AS r0 shard_key: est_rows:346,315 TableScan r0 storage:list stream:yes est_table_rows:346,315 GatherMerge partitions:all est_rows:50 alias:remote_0 Unfortunately, it seems to slow it down a bit further.Īpparently, it sorts in the end of the execution: Top offset:0 limit:50 75 ms (Visual Explain says 2 thanks for your suggestion. The index works great without the JOIN clause with an incredible speed of approx. 35M rows while the site_keyword for this particular query contains approx. The SQLite ORDER BY clause is used to sort the fetched data in ascending or descending order, based on one or more column. We tried with an index like alter table keywords add index (volume desc, id) but without any notable improvement.Ĭan anything be done to improve the first query? The keywords table contains approx. If the ORDER BY clause is skipped the query executes in approx. In MemSQL Studio the above query executes in approx. Generally, the SQLite tables will store data in unspecified. Inner join site_keyword on keywords.id = site_keyword.keyword_id and site_keyword.site_id = 1 In SQLite ORDER BY clause is used to sort column records either in ascending or descending order.

This is probably better explained with an example: select Is it possible to optimize a query with both JOINs and ORDER BY as long as the ORDER BY clause only targets columns from the first table?
