mysql - Optimizing COUNT(*) query with 4 MM relations and larger table -
I am struggling with the table with this question:
This is a main The table is product_table
which contains four mm links through lookup tables from mm1
to mm4
in the lookup tables uid_local
which contains the Product_table
and uid_foreign
are categories containing UID, which results in filtering.
After suggesting quasnois from the above question, which improves performance, the query looks like this:
SELECT COUNT (*) from product_table WHERE (product_table. Partner_id = 0 OR product_table .partner_id = 15) and present (Select zero from MM1 ou mm1.uid_local = product_table.uid and mm1.uid_foreign (10, 11, 12, 13, 14, 53)) and present (mm2 Select zero from mm2.uid_local = select the = product_table.uid and mm2.uid_foreign (7, 8, 9)) and present (select zero from mm3 where mm3.uid_local = product_table.uid and mm3.uid_foreign (51, 52)) and there is a selection of zero from MM4 (WHERE mm4.ui D_local = product_table.uid and mm4.uid_foreign IN (15, 16, 17, 18, 19))
This is to some extent about 10.000 lines by COUNT (*)
but it takes> 0.5 seconds to execute the query. It is very slow.
Exclinic
looks like this:
id select_type table type possible_keys key key_len ref quotes additional 1 primary product_table ref index new index 4 index 9430 Where use the 5 dependent subquery MM1 referee uid_local, uid_foreign uid_local 4 mm1.uid 5, where using 4 dependent subquery mm 2 referee uid_local, uid_foreign uid_local 4 mm2.uid 2 where 3 dependent subquery mm3 referee uid_local, uid_foreign uid_local 4 mm3.uid Use 3 where 2 dependent subquery MM4 referee ui D_local, uid_foreign uid_local 4mm4.uid 6 where
If I change the product_table.partner_id = 0
to do something, use that return lines. G. Some hundred or so queries are too fast (0.015 seconds).
The problem is that if the COUNT (*)
query is too fast if it has only a few lines (100-200), but a big result set (> 10.000 lines) Is very slow on the count.
- Some tables are indexed on
uid_local
anduid_foreign
- There is an indexed PK in
product_table
and there is another index onpartner_id
There are approximately 30,000 rows in the -
product_table
table <
Edit
< looks like (7, 8, 9)
Code IN
or other combination of numbers instead of code> IN (7, 9).
You can use the physical ideas of your existing parts to query. In this way, you can multiply the IN
statement in the same query after which you just need to create an index on uid
and the query should not take almost any time Product_table WHERE (product_table.partner_id = 0 or product_table.partner_id = 15) from SELECT COUNT (*) and present (vmm1 exists where vmm1.uid_local = product_table.uid) and (vmm2) Ou vmm2.uid_local = product_table.uid) and select (select zero from vmm3 where void zero to be selected by vmm3. Uid_local = product_table.uid) and exists (vm Select zero from m4 where vmm4.uid_local = product_table.uid)
The drawback is: If you change a lot of mm-tables, then the scene will need to be updated and which Will slow down the changes in those tables.
Comments
Post a Comment