日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 运维 最佳实践_Mysql 开发最佳实践

發布時間:2023/12/15 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 运维 最佳实践_Mysql 开发最佳实践 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.?Optimize Your Queries For the Query Cache

使用query cache來優化查詢

Most MySQL servers have query caching enabled. It’s one of the most effective methods of improving performance, that is quietly handled by the database engine. When the same query is executed multiple times, the result is fetched from the cache, which is quite fast.

大多數mysql服務器都開啟了query caching。這是提高性能的最直接有效的方法,由數據庫引擎直接實現。當同一個查詢被多次執行時,結果集可以直接從緩存中獲取,非常快速。

The main problem is, it is so easy and hidden from the programmer, most of us tend to ignore it. Some things we do can actually prevent the query cache from performing its task.

主要問題是,由于這個過于簡單,直接對程序員透明,我們經常會忽略它。有一些操作會造成querycache功能無法生效。

//?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'");

The reason query cache does not work in the first line is the usage of the CURDATE() function. This applies to all non-deterministic functions like NOW() and RAND() etc… Since the return result of the function can change, MySQL decides to disable query caching for that query. All we needed to do is to add an extra line of PHP before the query to prevent this from happening.

第一行的query cache無法工作的原因時使用了CURDATE()函數。這個同樣適用于所有的不確定性函數比如NOW()和RAND()等。。。由于返回的結果可能會變化,MYSQL決定對于查詢不使用query cache。解決這個問題的辦法就是在PHP中加入額外的一行。

2.?EXPLAIN Your SELECT Queries

解析SELECT查詢語句

Using the?EXPLAIN?keyword can give you insight on what MySQL is doing to execute your query. This can help you spot the bottlenecks and other problems with your query or table structures.

使用EXPLAIN關鍵字能夠讓你看到MYSQL在執行你的查詢時,內部的實際工作流程。這能夠幫助你定位到你的查詢或者表結構的瓶頸和其他問題。

The results of an EXPLAIN query will show you which indexes are being utilized, how the table is being scanned and sorted etc…

EXPLAIN的結果將展示給你哪些索引會被用到,表是怎么被掃描,排序等等。

Take a SELECT query (preferably a complex one, with joins), and add the keyword EXPLAIN in front of it. You can just use phpmyadmin for this. It will show you the results in a nice table. For example, let’s say I forgot to add an index to a column, which I perform joins on:

拿一個SELECT查詢(一個使用join的復雜查詢最好),在語句前面加上EXPLAIN.你可以使用phpmyadmin。他將結果以表的形式返回給你。比如,我在某個字段上面缺失了索引,下面執行了一個join:

After adding the index to the group_id field:

在字段group_id上添加索引后:

Now instead of scanning 7883 rows, it will only scan 9 and 16 rows from the 2 tables. A good rule of thumb is to multiply all numbers under the “rows” column, and your query performance will be somewhat proportional to the resulting number.

他將只掃描兩個表的9行和16行,而不是所有的7883行。一個很好的竅門就是將rows字段的所有數字相乘,你的查詢性能將會和這個數字成比例(注:對于這類聯合查詢適用)

3.?LIMIT 1 When Getting a Unique Row

查詢一行時用limit 1

Sometimes when you are querying your tables, you already know you are looking for just one row. You might be fetching a unique record, or you might just be just checking the existence of any number of records that satisfy your WHERE clause.

有些時候當你查詢表時,你已經知道你只會拿到一行。你可能是查看一個唯一記錄,或者你可能只是檢查滿足你的where條件

In such cases, adding LIMIT 1 to your query can increase performance. This way the database engine will stop scanning for records after it finds just 1, instead of going thru the whole table or index.

這種情況下,加上limit 1將提高你的效率。數據庫引擎在找到一條記錄后就停止繼續掃描,而不是掃完整張表或者索引。

//?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.?Index the Search Fields

在查詢的字段上加索引

Indexes are not just for the primary keys or the unique keys. If there are any columns in your table that you will search by, you should almost always index them.

索引并不只是為了主鍵或者唯一鍵。如果你的表中有任何字段需要被搜索,基本上你就需要增加索引。

As you can see, this rule also applies on a partial string search like “last_name LIKE ‘a%’”. When searching from the beginning of the string, MySQL is able to utilize the index on that column.

可以看到,這個規則也適用于partial string搜索比如"last_name LIKE ‘a%"。當從string的開頭開始搜索時,MYSQL能夠使用到這個字段上面的索引。

You should also understand which kinds of searches can not use the regular indexes. For instance, when searching for a word (e.g. “WHERE post_content LIKE ‘%apple%’”), you will not see a benefit from a normal index. You will be better off usingmysql fulltext searchor building your own indexing solution.

你也應該能夠理解哪種類型的搜索不能夠用到索引。比如,當檢索一個字(比如,?“WHERE post_content LIKE ‘%apple%’),正常的索引將不能夠幫助到你。你最好使用MYSQL全文檢索或者建立自己的索引解決方案。

5.?Index and Use Same Column Types for Joins

Joins的字段要用同樣的字段類型并建索引

If your application contains many JOIN queries, you need to make sure that the columns you join by are indexed on both tables. This affects how MySQL internally optimizes the join operation.

如果你的程序包含了多個JOIN 查詢,你需要確認你join的字段在兩個表中都被索引。這個會影響到MYSQL如何內部優化join操作。

Also, the columns that are joined, need to be the same type. For instance, if you join a DECIMAL column, to an INT column from another table, MySQL will be unable to use at least one of the indexes. Even the character encodings need to be the same type for string type columns.

另外,這些做了JOIN操作的字段,需要是同一種類型。比如,如果你將一個DECIMAL字段和一個INT字段做了關聯,MYSQL將至少不能用到一個索引。對于string類型,甚至字符編碼需要時同樣的類型。

//?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 ORDER BY RAND()

切忌ORDER BY RAND()

This is one of those tricks that sound cool at first, and many rookie programmers fall for this trap. You may not realize what kind of terrible bottleneck you can create once you start using this in your queries.

這個聽起來有點意思,很多新的程序員會掉進這個陷阱。你可能根本沒有意識到這是多么嚴重的瓶頸。

If you really need random rows out of your results, there are much better ways of doing it. Granted it takes additional code, but you will prevent a bottleneck that gets exponentially worse as your data grows. The problem is, MySQL will have to perform RAND() operation (which takes processing power) for every single row in the table before sorting it and giving you just 1 row.

如果你確實需要隨機的返回行,我們有更為有效的方法。雖然它多了一些代碼量,但是你能夠根本上解決掉這個隨著數據量增長而出現的瓶頸。問題就是,MYSQL將對于表中的所有行做RAND操作(耗cpu)然后才返回給你一行。

//?what?NOT?to?do:

$r=?mysql_query("S

總結

以上是生活随笔為你收集整理的mysql 运维 最佳实践_Mysql 开发最佳实践的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。