In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to improve the efficiency of MYSQL query, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The execution efficiency of MySQL database has a great influence on the execution speed of the program, so it is very useful to deal with and optimize the database effectively. Especially when a large amount of data needs to be processed.
1. Optimize your MySQL query cache
Query caching can be enabled by querying on the MySQL server. Letting the database engine process quietly in the background is one of the most effective ways to improve performance. When the same query is executed multiple times, it is quite fast if the result is extracted from the cache.
But the main problem is that it is so easy to hide that most of our programmers ignore it. In some processing tasks, we can actually prevent query caching from working.
/ / query cache does NOT work$r = mysql_query ("SELECT username FROM user WHERE signup_date > = CURDATE ()"); / / query cache works! $today = date ("Y-m-d"); $r = mysql_query ("SELECT username FROM user WHERE signup_date > ='$today'"); / / query cache does NOT work$r = mysql_query ("SELECT username FROM user WHERE signup_date > = CURDATE ()"); / / query cache works! $today = date ("Y-m-d") $r = mysql_query ("SELECT username FROM user WHERE signup_date > ='$today'")
two。 Use EXPLAIN to make your SELECT query clearer
Using the EXPLAIN keyword is another MySQL optimization technique that allows you to understand what query operations MySQL is doing, which can help you identify bottlenecks and show what went wrong with the query or table structure.
The results of the EXPLAIN query can tell you which indexes are being referenced, how the tables are scanned and sorted, and so on.
Implement a SELECT query (preferably a more complex one with joins mode) and add your keyword explanation to it. Here we can use phpMyAdmin, which will tell you the results in the table. For example, if I forget to add a column to an index when I am executing joins, EXPLAIN can help me find the problem.
3. Use LIMIT 1 to get a unique line
Sometimes, when you want to query a table, you know you only need to look at a row. You may go to a very unique record, or just happen to check the number of records that exist, and they all satisfy your WHERE clause.
In this case, adding a LIMIT 1 will make your query more efficient. In this way, the database engine will stop scanning when it finds only 1, instead of scanning the entire table or index.
/ / do I have any users from Alabama? / / what NOT to do: $r = mysql_query ("SELECT * FROM user WHERE state = 'Alabama'"); if (mysql_num_rows ($r) > 0) {/ /...} / / much better: $r = mysql_query ("SELECT 1 FROM user WHERE state =' Alabama' LIMIT 1"); if (mysql_num_rows ($r) > 0) {/ /.}
4. Retrieval fields in the index
An index is not just a primary or unique key. If you want to search for any column in the table, you should always point to the index.
5. Ensure that the index of the connection is of the same type
If the application contains multiple join queries, you need to make sure that the columns you link are indexed on both sides of the table. This affects how MySQL optimizes inner join operations.
In addition, the added columns must be of the same type. For example, if you add a DECIMAL column while adding an int column in another table, MySQL will not be able to use at least one of these metrics. Even if the character encoding must be of the same string type.
/ / looking for companies in my state $r = mysql_query ("SELECT company_name FROM users LEFT JOIN companies ON (users.state = companies.state) WHERE users.id = $user_id"); / / both state columns should be indexed / / and they both should be the same type and character encoding / / or MySQL might do full table scans
6. Do not use the BY RAND () command
This is a trap that many novice programmers will fall into. You may have unwittingly created a terrible peace. This trap is created when you use the BY RAND () command.
If you really need to display your results at random, there are many better ways to achieve it. It's true that this requires more code, but it avoids performance bottlenecks. The problem is that MySQL may execute the BY RAND () command for each separate row in the table (which consumes the processor's processing power) and then returns you only one row.
/ / what NOT to do: $r = mysql_query ("SELECT username FROM user ORDER BY RAND () LIMIT 1"); / / much better: $r = mysql_query ("SELECT count (*) FROM user"); $d = mysql_fetch_row ($r); $rand = mt_rand (0memento [0]-1); $r = mysql_query ("SELECT username FROM user LIMIT $rand, 1")
7. Avoid SELECT * commands as much as possible
The more data you read from the table, the slower the query becomes. He increases the amount of time the disk needs to operate, or if the database server is separate from the WEB server. You will experience a very long network delay simply because data is transferred unnecessarily between servers. It is a very good habit to always specify the columns you need.
/ / not preferred $r = mysql_query ("SELECT * FROM user WHERE user_id = 1"); $d = mysql_fetch_assoc ($r); echo "Welcome {$d ['username']}"; / / better: $r = mysql_query ("SELECT username FROM user WHERE user_id = 1"); $d = mysql_fetch_assoc ($r); echo "Welcome {$d [' username']}"; / / the differences are more significant with bigger result sets
8. Get advice from PROCEDURE ANALYSE ()
PROCEDURE ANALYSE () allows MySQL's column structure analysis and the actual data in the table to give you some advice. If you already have actual data in your table, it can serve your important decisions.
9. Prepared statement
Prepared statements can help you both in terms of performance optimization and security.
Prepared statements can effectively protect the application from SQL injection attacks by filtering bound variables by default. Of course, you can also filter manually, but because of the forgetful character of most programmers, it is difficult to achieve results.
/ create a prepared statement if ($stmt = $mysqli- > prepare ("SELECT username FROM user WHERE state=?")) {/ / bind parameters $stmt- > bind_param ("s", $state); / / execute $stmt- > execute (); / / bind result variables $stmt- > bind_result ($username); / / fetch value $stmt- > fetch (); printf ("% s is from% s\ n", $username, $state); $stmt- > close ();}
10. Store IP addresses as unsigned integers
Many programmers create a VARCHAR (15) without realizing that they can store IP addresses as integers. When you have an INT type, you only take up 4 bytes of space, which is a fixed size field.
You must make sure that the column you are operating on is of type UNSIGNED INT, because the IP address will use 32-bit unsigned integer.
1. $r = "UPDATE users SET ip = INET_ATON ('{$_ SERVER ['REMOTE_ADDR']}') WHERE user_id = $user_id"
There are many more query statements in MYSQL. Today we will talk about these 10 kinds.
Thank you for reading this article carefully. I hope the article "how to improve the efficiency of MYSQL query" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.