Use temporary tables with business features

Source: Internet
Author: User
Obtain a development SQL statement as follows: SELECTp. products_idFROMproductsASpJOINproducts_to_categoriesASpcUSING (products_id) JOINcategoriesAScUSING (categories_id) JOINproducts_r

Obtain a development SQL statement as follows: SELECTp. products_idFROMproductsASpJOINproducts_to_categoriesASpcUSING (products_id) JOINcategoriesAScUSING (categories_id) JOINproducts_r

Obtain a development SQL as follows:

SELECT p. products_id FROM products AS pJOIN products_to_categories AS pc USING (products_id) JOIN categories AS c USING (categories_id) JOIN products_realtime_quantity AS prq ON prq. sku_or_poa = p. products_modelWHERE products_status = 1 AND categories_status = 1 AND prq. msg! = 'Temporary out stock. 'order by p. products_date_added desc limit 10

Generally, we can see that such SQL statements only have status-like fields in the where clause (the selectivity is very low, and the data polarization is very obvious) and the order by statement is required, we should think of using force index (order_by_column) for optimization.

Explian

+ ---- + ------------- + ------- + -------- + Hour + ------------------- + --------- + hour + -------- + hour + | id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra | + ---- + ------------- + ------- + -------- + ------------------------------ + ------------------- + --------- + hour + -------- + hour + | 1 | SIMPLE | pc | index | PRIMARY, categories_id | PRIMARY | 8 | NULL | 1009510 | Using index; Using temporary; Using filesort | 1 | SIMPLE | p | eq_ref | PRIMARY, products_model | PRIMARY | 4 | banggood_work.pc.products_id | 1 | Using where | 1 | SIMPLE | c | eq_ref | PRIMARY | 4 | banggood_work.pc.categories_id | 1 | Using where | 1 | SIMPLE | prq | ref | ix_prg_sku_or_poa, ix_prq_msg | messages | 152 | banggood_work.p.products_model | 1 | Using where | + ---- + ------------- + ------- + -------- + messages + ----------------- + --------- + messages + -------- + messages +

I found that the mysql optimizer selects the primary key of the pc table. Although the index is used, the full index scan is performed, and the effect is still unsatisfactory!

After force index is used, explain

EXPLAIN-> SELECT p. products_id FROM products AS p force index (products_date_added)-> JOIN products_to_categories AS pc USING (products_id)-> JOIN categories AS c USING (categories_id)-> JOIN queries AS prq ON prq. sku_or_poa = p. products_model-> WHERE products_status = 1 AND categories_status = 1 AND prq. msg! = 'Temporary out stock. '-> order by p. products_date_added desc limit 10->; + ---- + ------------- + ------- + -------- + hour + --------------------- + --------- + hour + ------ + ------------- + | id | select_type | table | type | possible_keys | key | key_len | ref | rows extra | + ---- + ------------- + ------- + -------- + upper + ------------------- + --------- + upper + ------ + --------------- + | 1 | SIMPLE | p | index | NULL | products_date_added | 8 | NULL | 1 | Using where | 1 | SIMPLE | prq | ref | ix_prg_sku_or_poa, ix_prq_msg | ix_prg_sku_or_poa | 152 | banggood_work.p.products_model | 1 | Using where | 1 | SIMPLE | pc | ref | PRIMARY, categories_id | PRIMARY | 4 | banggood_work.p.products_id | 1009 | Using index | 1 | SIMPLE | c | eq_ref | PRIMARY | 4 | partition | 1 | Using where | + ---- + ------------- + ------- + -------- + ------------------------------ + --------------------- + ---------------------------------- + ------ + ------------- +

It is found that the index has changed to productsw_date_added, And the execution time is 2 s, and 0.003 s.


This is our consistent optimization method. However, we can optimize SQL statements with temporary tables based on their features and business features. Although it is not universal, however, it can broaden the thinking of SQL optimizers.

We can see that this statement needs to be matched according to the time added by the product (products_status = 1 AND categories_status = 1 AND prq. msg! = 'Temporary out stock. ') The 10 latest products are available. we know that the latest products are generally not immediately available, and the corresponding category id is also available, and the inventory is sufficient (otherwise, why should they be shelved ), this feature is more than 99.9%. therefore, we use this feature to first find out 200 products without any conditions from the product table, put them in a temporary table, and then use the temporary table result set to match with the condition, retrieve the latest 10 records.

(Article 1 is a reference value, which is obtained based on their respective logical characteristics)

The SQL statement is as follows:

Select distinct p. products_id FROM (SELECT products_id, products_model, products_status, specified productsORDER BY products_date_added desc limit 200) AS pJOIN products_to_categories AS pc USING (products_id) JOIN categories AS c USING (categories_id) JOIN products_realtime_quantity AS prq ON prq. sku_or_poa = p. products_modelWHERE products_status = 1 AND categories_status = 1 AND prq. msg! = 'Temporary out stock. 'order by products_date_added desc limit 10;

After explaining, we found

| 1 | PRIMARY | | ALL | NULL | 200 | Using where; Using temporary; Using filesort | 1 | PRIMARY | prq | ref | ix_prg_sku_or_poa, ix_prq_msg | ix_prg_sku_or_poa | 152 | p. products_model | 1 | Using where; Distinct | 1 | PRIMARY | pc | ref | PRIMARY, categories_id | PRIMARY | 4 | p. products_id | 1009 | Using index; Distinct | 1 | PRIMARY | c | eq_ref | PRIMARY | 4 | banggood_work.pc.categories_id | 1 | Using where; distinct | 2 | DERIVED | products | index | NULL | products_date_added | 8 | NULL | 200 |

We can see that 200 entries have been sorted using the products_date_added field in the products table. The entire SQL statement is converted into a temporary table with only 200 rows for query. The performance is relative to the original statement, improved by hundreds of times!

The execution time is about 0.02 s (may be slightly slower than the force index ).

This article is from the "original" blog. Please keep this source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.