Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of MySQL 10 million level big data SQL query optimization?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about MySQL tens of millions of big data SQL query optimization knowledge points to share with you. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. In order to optimize the query, we should avoid full table scanning as far as possible, and we should first consider establishing indexes on the columns involved in where and order by.

two。 Try to avoid judging the null value of the field in the where clause, otherwise it will cause the engine to give up using the index and do a full table scan. For example, select id from t where num is null can set the default value 0 on num to ensure that the num column in the table has no null value, and then query it like this: select id from t where num=0

3. Avoid using the! = or operator in the where clause as much as possible, otherwise the engine will give up using the index and do a full table scan.

4. Try to avoid using or to join conditions in the where clause, otherwise it will cause the engine to give up using the index and do a full table scan, for example: select id from t where num=10 or num=20 can query: select id from t where num=10 union all select id from t where num=20

5.in and not in should also be used with caution, otherwise it will lead to full table scanning, such as: select id from t where num in (1 and 2) for consecutive values, do not use in if you can use between: select id from t where num between 1 and 3

6. The following query will also result in a full table scan: select id from t where name like'% Lee%'to improve efficiency, consider full-text retrieval.

7. Using parameters in the where clause also results in a full table scan. Because SQL parses local variables only at run time, the optimizer cannot defer the choice of an access plan until run time; it must be selected at compile time. However, if an access plan is established at compile time, the value of the variable is still unknown and cannot be used as an input to the index selection. The following statement will scan the full table: select id from t where num=@num can instead force the query to use the index: select id from t with (index (index name)) where num=@num

8. Expression manipulation of fields in the where clause should be avoided as far as possible, which will cause the engine to abandon the use of indexes and perform full table scans. For example, select id from t where num/2=100 should be changed to: select id from t where num=100*2.

9. Functional manipulation of fields in the where clause should be avoided as far as possible, which will cause the engine to abandon the use of indexes and perform full table scans. For example: select id from t where substring (name,1,3) = 'abc', the id that name begins with abc should be changed to: select id from t where name like' abc%'.

10. Do not perform functions, arithmetic operations, or other expression operations to the left of the "=" in the where clause, or the system may not be able to use the index correctly.

11. When using an index field as a condition, if the index is a composite index, the first field in the index must be used as a condition to ensure that the system uses the index, otherwise the index will not be used. and the order of the fields should be consistent with the order of the index as far as possible.

twelve。 Do not write some meaningless queries, if you need to generate an empty table structure: select col1,col2 into # t from t where 1: 0, this kind of code will not return any result sets, but will consume system resources, it should be changed to: create table # t (…) .

13. In many cases, using exists instead of in is a good choice: select num from a where num in (select num from b), replace it with the following statement: select num from a where exists (select 1 from b where num=a.num).

14. Not all indexes are valid for the query. SQL optimizes the query according to the data in the table. When there are a large number of duplicate data in the index column, the SQL query may not use the index. For example, if there are fields sex,male and female in a table, then even if the index is built on the sex, it will not play a role in the query efficiency.

15. Index is not the more the better, the index can improve the efficiency of the corresponding select, but also reduce the efficiency of insert and update, because insert or update may rebuild the index, so how to build the index needs to be carefully considered, depending on the specific situation. It is best to have no more than 6 indexes in a table, and if there are too many, consider whether it is necessary to build indexes on some infrequently used columns.

16. Updating clustered index data columns should be avoided as much as possible, because the order of clustered index data columns is the physical storage order of table records. Once the value of this column changes, it will lead to the adjustment of the order of the whole table records, which will consume a lot of resources. If the application system needs to update the clustered index data column frequently, it needs to consider whether the index should be built as a clustered index.

17. Try to use numeric fields, and try not to design character fields that contain only numeric information, which will reduce the performance of queries and connections, and increase storage overhead. This is because the engine compares each character in the string one by one when processing queries and connections, while for numeric types, it only needs to be compared once.

18. Use varchar/nvarchar instead of char/nchar as much as possible, because first of all, the storage space of longer fields is small, which can save storage space, and secondly, for queries, searching in a relatively small field is obviously more efficient.

19. Don't use select * from t anywhere, replace "*" with a specific list of fields, and don't return any fields that you don't need.

20. Try to use table variables instead of temporary tables. If the table variable contains a large amount of data, note that the index is very limited (only the primary key index).

21. Avoid creating and deleting temporary tables frequently to reduce the consumption of system table resources.

twenty-two。 Temporary tables are not unavailable, and using them appropriately can make some routines more efficient, for example, when you need to re-reference a dataset in a large or common table. However, for one-time events, it is best to use an export table.

23. When creating a new temporary table, if you insert a large amount of data at one time, you can use select into instead of create table to avoid causing a lot of log to speed up; if the amount of data is small, in order to ease the resources of the system table, you should first create table, and then insert.

24. If temporary tables are used, be sure to explicitly delete all temporary tables at the end of the stored procedure, first truncate table, and then drop table, to avoid prolonged locking of system tables.

25. Avoid using cursors as much as possible, because cursors are inefficient, and if you operate on cursors with more than 10,000 rows of data, you should consider rewriting.

twenty-six。 Before using a cursor-based or temporary table approach, you should look for a set-based solution to the problem, which is usually more effective.

twenty-seven。 Like temporary tables, cursors are not unavailable. Using FAST_FORWARD cursors for small datasets is generally better than other row-by-row processing methods, especially if you have to reference several tables to get the data you need. Routines that include "totals" in the result set are usually executed faster than using cursors. If development time permits, you can try both the cursor-based approach and the set-based approach to see which method works better.

twenty-eight。 Set SET NOCOUNT ON at the beginning of all stored procedures and triggers and set SET NOCOUNT OFF at the end. There is no need to send a DONE_IN_PROC message to the client after each statement of the stored procedure and trigger is executed.

twenty-nine。 Try to avoid large transaction operations and improve the concurrency ability of the system.

thirty。 Try to avoid returning a large amount of data to the client. If the amount of data is too large, you should consider whether the corresponding requirements are reasonable.

Thank you for reading! About "what are the knowledge points of big data query optimization at tens of millions of levels of MySQL?" this article ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report