Ten Suggestions for improving database speed and Ten Suggestions for database Learning
Important information of many websites is stored in the database. Users can obtain user information by submitting and accessing the database. If the database speed is very fast, it will help to save server resources. In this article, I collected ten skills to optimize the database speed.
0. carefully design the database
The first trick may seem to be taken for granted, but in fact most database problems come from poorly designed database structures.
For example, I used to store client information and payment information in the same database column. This is terrible for system and database developers.
When creating a database, you should store the information in different tables and tables, adopt the standard naming method, and adopt the primary key.
Source: http://www.simple-talk.com/ SQL /database-administration/ten-common-database-design-mistakes/
1. Know where you need Optimization
If you want to optimize a query statement, it is very helpful to know the result of this statement clearly. Using the EXPLAIN statement, you will get a lot of useful information. The following is an example:
EXPLAIN SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column;
Source: http://dev.mysql.com/doc/refman/5.0/en/using-explain.html
2. The fastest query statement... Are the statements you haven't sent
Every time you send a statement to the database, you will use a lot of server resources. Therefore, the best way to cache your query statements on a website with high traffic is.
There are many cache statement methods, which are listed below:
AdoDB: AdoDB is a simplified PHP database. With it, you can choose different database systems (MySQL, PostGreSQL, Interbase, and so on), and it is designed for speed. AdoDB provides a simple but powerful cache system. Also, AdoDB has a BSD license and you can use it for free in your project. For commercial projects, it also has LGPL licenses.
Memcached: Memcached is a distributed memory cache system that reduces database load and accelerates dynamic database-based websites.
CSQL Cache: CSQL Cache is an open-source data Cache architecture. I have never tried it, but it looks great.
3. Don't select what you don't need
To obtain the desired data, a very common way is to use the * character, which will list all columns.
SELECT * FROM wp_posts;
However, you should only list the columns you need, as shown below. If you visit a very small website, for example, one user per minute, there may be no difference. However, if a large-volume website such as Cats Who Code is used, this saves a lot of trouble for the database.
SELECT title, excerpt, author FROM wp_posts;
4. Use LIMIT
It is very common to obtain only data of a specific number of rows. For example, a blog displays only 10 articles per page. In this case, you should use LIMIT to LIMIT the number of data rows you want to select.
If there is no LIMIT and the table has 100,000 rows of data, you will traverse all the rows, which is unnecessary for the server.
SELECT title, excerpt, author FROM wp_posts LIMIT 10;
5. Avoid loop queries
When using SQL in PHP, you can place the SQL statement in a loop statement. But doing so adds load to your database.
The following example illustrates the problem of "nested query statements in loop statements:
foreach ($display_order as $id => $ordinal) {$sql = "UPDATE categories SET display_order = $ordinal WHERE id = $id";mysql_query($sql);}
You can do this:
UPDATE categoriesSET display_order = CASE idWHEN 1 THEN 3WHEN 2 THEN 4WHEN 3 THEN 5ENDWHERE id IN (1,2,3)
Source: http://www.karlrixon.co.uk/articles/ SQL /update-multiple-rows-with-different-values-and-a-single-sql-query/
6. Use join to replace subqueries
As a programmer, subqueries are something that you can be tempted to use and abuse. Subqueries, as show below, can be very useful:
Programmers may like to use subqueries or even abuse them. The following subqueries are very useful:
SELECT a.id,(SELECT MAX(created)FROM postsWHERE author_id = a.id)AS latest_post FROM authors a
Although a subquery is useful, a join statement can be used to replace it, And the join statement can be executed more quickly.
SELECT a.id, MAX(p.created) AS latest_postFROM authors aINNER JOIN posts pON (a.id = p.author_id)GROUP BY a.id
Source: http://20bits.com/articles/10-tips-for-optimizing-mysql-queries-that-dont-suck/
7. Be careful when using wildcards
Wildcard is very easy to use. When searching data, you can use a wildcard to replace one or more characters. I am not saying that it cannot be used, but should be used with caution, and do not use full wildcard. prefix or post-wildcard can complete the same task.
In fact, using full-word wildcard characters to search for millions of data sets your database as a machine.
#Full wildcardSELECT * FROM TABLE WHERE COLUMN LIKE '%hello%';#Postfix wildcardSELECT * FROM TABLE WHERE COLUMN LIKE 'hello%';#Prefix wildcardSELECT * FROM TABLE WHERE COLUMN LIKE '%hello';
Source: http://hungred.com/useful-information/ways-optimize-sql-queries/
8. Use UNION to replace OR
The following example uses the OR statement:
SELECT * FROM a, b WHERE a.p = b.q or a.x = b.y;
UNION statement. You can combine the results of two or more select statements. The results returned in the following example are the same as above, but the speed is faster:
SELECT * FROM a, b WHERE a.p = b.qUNIONSELECT * FROM a, b WHERE a.x = b.y
Source: http://www.bcarter.com/optimsql.htm
9. Use Indexes
The database index is similar to the index you see in the Library: it allows you to quickly obtain the desired information, and the index in the library allows readers to quickly find the desired book.
You can create an index on one or more columns. An index is a data structure that organizes values of one or more columns in a table in a specific order.
The following statement creates an index on the Model column of the Product table. The index name is idxModel.
CREATE INDEX idxModel ON Product (Model);
Source: http://www.sql-tutorial.com/sql-indexes-sql-tutorial/
The above tutorial provides ten suggestions for improving the database speed based on database learning suggestions.