Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am afraid that if you must allow your users to sort on any field (and have this sort use an index) then you need an index for each possible sort. It is impossible to do otherwise by definition. Sorting on a given row may only make use of an index on this row.</p> <p>I see little alternatives here. Either reduce the number of rows to be sorted (25k lines is a bit large a result set, do your users really need that many lines?) or do not allow sorts on all rows.</p> <p>Notice that a query will usually <em>not</em> be able to use more than one index by table. As advised by others, a compound index is better for the query you mentionned, although I would rather advise the opposite order (<code>(guid, date)</code>) (the query first needs to select each <code>guid</code>, and then, for each of them, sort the corresponding rows).</p> <p>Also add an index on <code>line(guid, acompte, invoice)</code>.</p> <p>(above suggestions on indexing assume MyISAM tables)</p> <p>In terms of optimisation of the query itself, there is little moreto be done, considering the simple execution plan.</p> <p>You might get better results with this version, or you might not:</p> <pre><code>SELECT invoice.*, -- DISTINCT is redudant here because of the GROUP BY clause GROUP_CONCAT(ligne_acompte.guid) as line -- DISTINCT is (presumably) redundant here because guid is (presumably) unique FROM invoice LEFT JOIN ( SELECT guid, invoice FROM line WHERE acompte = 1 ) AS ligne_acompte ON ligne_acompte.invoice = invoice.guid GROUP BY invoice.guid ORDER BY invoice.date; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload