Changes between Version 1 and Version 2 of Ticket #34811, comment 2


Ignore:
Timestamp:
Sep 2, 2023, 9:04:01 PM (15 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34811, comment 2

    v1 v2  
    11If that can be of any help, we've observed that MySQL's query planer can be pretty bad at using composite indexes when `OR` conditions are involved and had to resort to using `UNION` to target them.
    22
    3 Re-writing your lookup to move the `filling_id=pk` outside of the `OR` should help MySQL at figuring out that the `filing_id` index can be used at least.
     3Re-writing your lookup to move the `filling_id=pk` outside of the `OR`, since it's used by both, might help MySQL figuring out that the `filing_id` index can be used at least.
    44
    55{{{#!python
     
    2121        OR `taxobjects_taxinput` . `deleted` > ?
    2222    )
    23 LIMIT
    24   ?
    2523}}}
     24
     25Otherwise I'm pretty sure that using `union` will perform great but it comes with some ORM limitations #28519.
     26
     27{{{#!python
     28TaxInput.objects.filter(
     29    filling_id=pk,
     30    updated_at__gt=last_engine.created_at,
     31).union(
     32    TaxInput.objects.filter(
     33        filling_id=pk,
     34        deleted__gt=last_engine.created_at,
     35    )
     36)
     37}}}
     38
     39{{{#!sql
     40SELECT
     41  *
     42FROM
     43  `taxobjects_taxinput`
     44WHERE
     45    `taxobjects_taxinput` . `filling_id` = ?
     46    AND `taxobjects_taxinput` . `updated_at` > ?
     47UNION
     48SELECT
     49  *
     50FROM
     51  `taxobjects_taxinput`
     52WHERE
     53    `taxobjects_taxinput` . `filling_id` = ?
     54    AND `taxobjects_taxinput` . `deletedt` > ?
     55}}}
Back to Top