mysql relational query optimization

Online data found that the phenomenon of a large amount of data waiting explain discover problems through the sql wording, simply record it here.

Business scenario is this:

The presence of the shopping cart and the cost of two tables, shopping cart data is generated when the purchase of goods, purchase of goods for recording data, also purchased will generate fee schedule, for a total expenditure of Statistics Goods (equivalent to orders), shopping the relationship between the car and the cost is many, by gg_gwc.fyxh gg_fei.id association =. It should be updated to meet the conditions of the cart fei associated data.

  • The original query:

Because the test environment, the actual execution time is not long, but gg_fei table id found that although the use of the index, but the query is whole table query, because in conditions using subqueries can cause failure of the index;

  • Optimization try way
  • Mode 1:

The actual results displayed in this manner is a failure because it did not change fundamentally in (subquery) this way, the idea of ​​it is to reduce the sub-query data to speed up access sql, sql simple from this point of view, actual indeed a bit role, he will be limited to eight subquery two; but first sql because the conditions are very precise, it is actually only one query;

  • Option 2:

  • Mode 3:

Guess you like

Origin www.cnblogs.com/chengmuyu/p/11499083.html