如何在Ubuntu 16.04上使用ProxySQL缓存优化MySQL查询
The author selected the Free Software Foundation to receive a donation as part of the Write for DOnations program.
作者選擇了自由軟件基金會作為Write for DOnations計劃的一部分接受捐贈。
介紹 (Introduction)
ProxySQL is a SQL-aware proxy server that can be positioned between your application and your database. It offers many features, such as load-balancing between multiple MySQL servers and serving as a caching layer for queries. This tutorial will focus on ProxySQL’s caching feature, and how it can optimize queries for your MySQL database.
ProxySQL是可識別 SQL的代理服務器,可以位于應用程序和數據庫之間。 它提供了許多功能,例如多個MySQL服務器之間的負載平衡以及充當查詢的緩存層 。 本教程將重點介紹ProxySQL的緩存功能,以及如何優化MySQL數據庫的查詢。
MySQL caching occurs when the result of a query is stored so that, when that query is repeated, the result can be returned without needing to sort through the database. This can significantly increase the speed of common queries. But in many caching methods, developers must modify the code of their application, which could introduce a bug into the codebase. To avoid this error-prone practice, ProxySQL allows you to set up transparent caching.
MySQL緩存是在存儲查詢結果時發生的,因此,當重復查詢時,可以返回結果而無需對數據庫進行排序。 這可以大大提高常見查詢的速度。 但是在許多緩存方法中,開發人員必須修改其應用程序的代碼,這可能會在代碼庫中引入錯誤。 為了避免這種容易出錯的做法,ProxySQL允許您設置透明緩存 。
In transparent caching, only database administrators need to change the ProxySQL configuration to enable caching for the most common queries, and these changes can be done through the ProxySQL admin interface. All the developer needs to do is connect to the protocol-aware proxy, and the proxy will decide if the query can be served from the cache without hitting the back-end server.
在透明緩存中,只有數據庫管理員需要更改ProxySQL配置才能為最常見的查詢啟用緩存,并且這些更改可以通過ProxySQL管理界面完成。 開發人員所需要做的就是連接到支持協議的代理,代理將決定是否可以從緩存中提供查詢而不訪問后端服務器。
In this tutorial, you will use ProxySQL to set up transparent caching for a MySQL server on Ubuntu 16.04. You will then test its performance using mysqlslap with and without caching to demonstrate the effect of caching and how much time it can save when executing many similar queries.
在本教程中,您將使用ProxySQL在Ubuntu 16.04上為MySQL服務器設置透明緩存。 然后,您將使用帶有和不帶有緩存的mysqlslap來測試其性能,以演示緩存的效果以及執行許多類似查詢時可以節省多少時間。
先決條件 (Prerequisites)
Before you begin this guide you’ll need the following:
在開始本指南之前,您需要滿足以下條件:
One Ubuntu 16.04 server with at least 2 GB of RAM, set up with a non-root user with sudo privileges and a firewall, as instructed in our Ubuntu 16.04 Initial Server Setup guide.
一臺至少具有2 GB RAM的Ubuntu 16.04服務器,由具有sudo特權的非root用戶和防火墻設置,如《 Ubuntu 16.04初始服務器設置指南》中所述 。
第1步-安裝和設置MySQL服務器 (Step 1 — Installing and Setting Up the MySQL Server)
First, you will install MySQL server and configure it to be used by ProxySQL as a back-end server for serving client queries.
首先,您將安裝MySQL服務器并將其配置為由ProxySQL用作后端服務器以服務于客戶端查詢。
On Ubuntu 16.04, mysql-server can be installed using this command:
在Ubuntu 16.04上,可以使用以下命令安裝mysql-server :
- sudo apt-get install mysql-server 須藤apt-get install mysql-server
Press Y to confirm the installation.
按Y確認安裝。
You will then be prompted for your MySQL root user password. Enter a strong password and save it for later use.
然后將提示您輸入MySQL root用戶密碼。 輸入一個強密碼并保存以備后用。
Now that you have your MySQL server ready, you will configure it for ProxySQL to work correctly. You need to add a monitor user for ProxySQL to monitor the MySQL server, since ProxySQL listens to the back-end server via the SQL protocol, rather than using a TCP connection or HTTP GET requests to make sure that the backend is running. monitor will use a dummy SQL connection to determine if the server is alive or not.
現在您已經準備好了MySQL服務器,您將對其進行配置,以使ProxySQL正常工作。 您需要為ProxySQL添加一個監視用戶來監視MySQL服務器,因為ProxySQL通過SQL協議(而不是使用TCP連接或HTTP GET請求來確保后端正在運行)偵聽后端服務器。 Monitor將使用虛擬SQL連接來確定服務器是否處于活動狀態。
First, log in to the MySQL shell:
首先,登錄到MySQL shell:
- mysql -uroot -p mysql -uroot -p
-uroot logs you in using the MySQL root user, and -p prompts for the root user’s password. This root user is different from your server’s root user, and the password is the one you entered when installing the mysql-server package.
-uroot記錄您在使用MySQL root用戶,并-p提示輸入root用戶的密碼。 該root用戶不同于服務器的root用戶,密碼是您在安裝mysql-server軟件包時輸入的密碼。
Enter the root password and press ENTER.
輸入root密碼,然后按ENTER 。
Now you will create two users, one named monitor for ProxySQL and another that you will use to execute client queries and grant them the right privileges. This tutorial will name this user sammy.
現在,您將創建兩個用戶,一個用戶名為ProxySQL的監視器 ,另一個用戶將用于執行客戶端查詢并為其授予正確的權限。 本教程將將此用戶命名為sammy 。
Create the monitor user:
創建監視器用戶:
CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor_password';
創建用戶'monitor'@'%'由' monitor_password '標識;
The CREATE USER query is used to create a new user that can connect from specific IPs. Using % denotes that the user can connect from any IP address. IDENTIFIED BY sets the password for the new user; enter whatever password you like, but make sure to remember it for later use.
CREATE USER查詢用于創建可以從特定IP連接的新用戶。 使用%表示用戶可以從任何IP地址進行連接。 IDENTIFIED BY設置新用戶的密碼; 輸入您喜歡的任何密碼,但請務必記住該密碼以備后用。
With the user monitor created, next make the sammy user:
創建了用戶監視器后 ,接下來使該用戶變得笨拙 :
CREATE USER 'sammy'@'%' IDENTIFIED BY 'sammy_password';
創建用戶' sammy '@'%'由' sammy_password '標識;
Next, grant privileges to your new users. Run the following command to configure monitor:
接下來,向您的新用戶授予特權。 運行以下命令以配置監視器 :
- GRANT SELECT ON sys.* TO 'monitor'@'%'; SYS。*上的GRANT SELECT * TO'monitor'@'%';
The GRANT query is used to give privileges to users. Here you granted only SELECT on all tables in the sys database to the monitor user; it only needs this privilege to listen to the back-end server.
GRANT查詢用于向用戶授予特權。 在這里,您僅將sys數據庫中所有表上的SELECT授予了監視用戶。 它只需要此特權即可偵聽后端服務器。
Now grant all privileges to all databases to the user sammy:
現在,將所有數據庫的所有特權授予用戶sammy :
GRANT ALL PRIVILEGES on *.* TO 'sammy'@'%';
GRANT ALL對*特權* TO ' 森 '@' %'。
This will allow sammy to make the necessary queries to test your database later.
這將使sammy進行必要的查詢,以便以后測試數據庫。
Apply the privilege changes by running the following:
通過運行以下命令來應用特權更改:
- FLUSH PRIVILEGES; 沖洗特權;
Finally, exit the mysql shell:
最后,退出mysql shell:
- exit; 出口;
You’ve now installed mysql-server and created a user to be used by ProxySQL to monitor your MySQL server, and another one to execute client queries. Next you will install and configure ProxySQL.
現在,您已經安裝了mysql-server并創建了一個供ProxySQL用來監視您MySQL服務器的用戶,以及一個用于執行客戶端查詢的用戶。 接下來,您將安裝和配置ProxySQL。
第2步-安裝和配置ProxySQL Server (Step 2 — Installing and Configuring ProxySQL Server)
Now you can install ProxySQL server, which will be used as a caching layer for your queries. A caching layer exists as a stop between your application servers and database back-end servers; it is used to connect to the database and to save the results of some queries in its memory for fast access later.
現在,您可以安裝ProxySQL服務器,該服務器將用作查詢的緩存層 。 緩存層是您的應用程序服務器和數據庫后端服務器之間的站點。 它用于連接數據庫,并將某些查詢的結果保存在其內存中,以便以后快速訪問。
The ProxySQL releases Github page offers installation files for common Linux distributions. For this tutorial, you will use wget to download the ProxySQL version 2.0.4 Debian installation file:
ProxySQL發行的Github頁提供了常見Linux發行版的安裝文件。 對于本教程,您將使用wget下載ProxySQL版本2.0.4 Debian安裝文件:
wget https://github.com/sysown/proxysql/releases/download/v2.0.4/proxysql_2.0.4-ubuntu16_amd64.deb
wget https://github.com/sysown/proxysql/releases/download/v 2.0.4 / proxysql_ 2.0.4 -ubuntu16_amd64.deb
Next, install the package using dpkg:
接下來,使用dpkg安裝軟件包:
sudo dpkg -i proxysql_2.0.4-ubuntu16_amd64.deb
須藤dpkg -i proxysql_ 2.0.4 -ubuntu16_amd64.deb
Once it is installed, start ProxySQL with this command:
安裝完成后,使用以下命令啟動ProxySQL:
- sudo systemctl start proxysql sudo systemctl啟動proxysql
You can check if ProxySQL started correctly with this command:
您可以使用以下命令檢查ProxySQL是否正確啟動:
- sudo systemctl status proxysql sudo systemctl狀態proxysql
You will get an output similar to this:
您將獲得類似于以下的輸出:
Outputroot@ubuntu-s-1vcpu-2gb-sgp1-01:~# systemctl status proxysql ● proxysql.service - LSB: High Performance Advanced Proxy for MySQLLoaded: loaded (/etc/init.d/proxysql; bad; vendor preset: enabled)Active: active (exited) since Wed 2019-06-12 21:32:50 UTC; 6 months 7 days agoDocs: man:systemd-sysv-generator(8)Tasks: 0Memory: 0BCPU: 0Now it is time to connect your ProxySQL server to the MySQL server. For this purpose, use the ProxySQL admin SQL interface, which by default listens to port 6032 on localhost and has admin as its username and password.
現在是時候將ProxySQL服務器連接到MySQL服務器了。 為此,請使用ProxySQL admin SQL接口,默認情況下,該接口偵聽localhost上的端口6032 ,并將admin作為用戶名和密碼。
Connect to the interface by running the following:
通過運行以下命令連接到界面:
- mysql -uadmin -p -h 127.0.0.1 -P6032 mysql -uadmin -p -h 127.0.0.1 -P6032
Enter admin when prompted for the password.
當提示您輸入密碼時,輸入admin 。
-uadmin sets the username as admin, and the -h flag specifies the host as localhost. The port is 6032, specified using the -P flag.
-uadmin將用戶名設置為admin , -h標志將主機指定為localhost 。 端口是6032 ,使用-P標志指定。
Here you had to specify the host and port explicitly because, by default, the MySQL client connects using a local sockets file and port 3306.
在這里,您必須明確指定主機和端口,因為默認情況下,MySQL客戶端使用本地套接字文件和端口3306 。
Now that you are logged into the mysql shell as admin, configure the monitor user so that ProxySQL can use it. First, use standard SQL queries to set the values of two global variables:
現在,您以admin身份登錄到mysql shell,配置監視器用戶,以便ProxySQL可以使用它。 首先,使用標準SQL查詢來設置兩個全局變量的值:
- UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username'; 更新global_variables SET variable_value ='monitor'WHERE variable_name ='mysql-monitor_username';
UPDATE global_variables SET variable_value='monitor_password' WHERE variable_name='mysql-monitor_password';
UPDATE global_variables SET variable_value =' monitor_password'WHERE variable_name ='mysql-monitor_password';
The variable mysql-monitor_username specifies the MySQL username that will be used to check if the back-end server is alive or not. The variable mysql-monitor_password points to the password that will be used when connecting to the back-end server. Use the password you created for the monitor username.
變量mysql-monitor_username指定MySQL用戶名,該用戶名將用于檢查后端服務器是否處于活動狀態。 變量mysql-monitor_password指向連接到后端服務器時將使用的密碼。 使用您為監視器用戶名創建的密碼。
Every time you create a change in the ProxySQL admin interface, you need to use the right LOAD command to apply changes to the running ProxySQL instance. You changed MySQL global variables, so load them to RUNTIME to apply changes:
每次在ProxySQL管理界面中創建更改時,都需要使用正確的LOAD命令將更改應用于正在運行的ProxySQL實例。 您更改了MySQL全局變量,因此將它們加載到RUNTIME以應用更改:
- LOAD MYSQL VARIABLES TO RUNTIME; 將MYSQL變量加載到運行時;
Next, SAVE the changes to the on-disk database to persist changes between restarts. ProxySQL uses its own SQLite local database to store its own tables and variables:
接下來,將更改SAVE到磁盤數據庫,以在兩次重新啟動之間保留更改。 ProxySQL使用其自己的SQLite本地數據庫來存儲其自己的表和變量:
- SAVE MYSQL VARIABLES TO DISK; 將MYSQL變量保存到磁盤;
Now, you will tell ProxySQL about the back-end server. The table mysql_servers holds information about each back-end server where ProxySQL can connect and execute queries, so add a new record using a standard SQL INSERT statement with the following values for hostgroup_id, hostname, and port:
現在,您將告訴ProxySQL有關后端服務器。 mysql_servers表保存有關ProxySQL可以連接和執行查詢的每個后端服務器的信息,因此,使用標準SQL INSERT語句添加新記錄,并為hostgroup_id , hostname和port設置以下值:
- INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (1, '127.0.0.1', 3306); 插入mysql_servers(hostgroup_id,主機名,端口)VALUES(1,'127.0.0.1',3306);
To apply the changes, run LOAD and SAVE again:
要應用更改,請再次運行LOAD和SAVE :
- LOAD MYSQL SERVERS TO RUNTIME; 將MYSQL服務器加載到運行時;
- SAVE MYSQL SERVERS TO DISK; 保存MYSQL服務器到磁盤;
Finally, you will tell ProxySQL which user will connect to the back-end server; set sammy as the user, and replace sammy_password with the password you created earlier:
最后,您將告訴ProxySQL哪個用戶將連接到后端服務器。 將sammy設置為用戶,并將sammy_password替換為您先前創建的密碼:
INSERT INTO mysql_users(username, password, default_hostgroup) VALUES ('sammy', 'sammy_password', 1);
插入mysql_users(用戶名,密碼,default_hostgroup)值('sammy',' sammy_password ',1);
The table mysql_users holds information about users used to connect to the back-end servers; you specified the username, password, and default_hostgroup.
mysql_users表保存有關用于連接到后端服務器的用戶的信息。 您指定了username , password和default_hostgroup 。
LOAD and SAVE the changes:
LOAD并SAVE更改:
- LOAD MYSQL USERS TO RUNTIME; 將MYSQL用戶加載到運行時;
- SAVE MYSQL USERS TO DISK; 將MYSQL用戶保存到磁盤;
Then exit the mysql shell:
然后退出mysql shell:
- exit; 出口;
To test that you can connect to your back-end server using ProxySQL, execute the following test query:
要測試您是否可以使用ProxySQL連接到后端服務器,請執行以下測試查詢:
- mysql -usammy -h127.0.0.1 -p -P6033 -e "SELECT @@HOSTNAME as hostname" mysql -usammy -h127.0.0.1 -p -P6033 -e“選擇@@ HOSTNAME作為主機名”
In this command, you used the -e flag to execute a query and close the connection. The query prints the hostname of the back-end server.
在此命令中,您使用了-e標志來執行查詢并關閉連接。 該查詢將打印后端服務器的主機名。
Note: ProxySQL uses port 6033 by default for listening to incoming connections.
注意:默認情況下,ProxySQL使用端口6033偵聽傳入的連接。
The output will look like this, with your_hostname replaced by your hostname:
輸出將如下所示,將your_hostname替換為您的主機名:
Output+----------------------------+ | hostname | +----------------------------+ | your_hostname | +----------------------------+To learn more about ProxySQL configuration, see Step 3 of How To Use ProxySQL as a Load Balancer for MySQL on Ubuntu 16.04.
要了解有關ProxySQL配置的更多信息,請參閱在Ubuntu 16.04上如何將ProxySQL用作MySQL的負載均衡器的步驟3 。
So far, you configured ProxySQL to use your MySQL server as a backend and connected to the backend using ProxySQL. Now, you are ready to use mysqlslap to benchmark the query performance without caching.
到目前為止,您已將ProxySQL配置為使用MySQL服務器作為后端,并使用ProxySQL連接到后端。 現在,您可以使用mysqlslap來對查詢性能進行基準測試,而無需進行緩存。
第3步-使用不帶緩存的mysqlslap測試 (Step 3 — Testing Using mysqlslap Without Caching)
In this step, you will download a test database so you can execute queries against it with mysqlslap to test the latency without caching, setting a benchmark for the speed of your queries. You will also explore how ProxySQL keeps records of queries in the stats_mysql_query_digest table.
在此步驟中,您將下載一個測試數據庫,以便您可以使用mysqlslap執行查詢以測試延遲而無需進行緩存,從而設置查詢速度的基準。 您還將探索ProxySQL如何將查詢記錄保存在stats_mysql_query_digest表中。
mysqlslap is a load emulation client that is used as a load testing tool for MySQL. It can test a MySQL server with auto-generated queries or with some custom queries executed on a database. It comes installed with the MySQL client package, so you do not need to install it; instead, you will download a database for testing purposes only, on which you can use mysqlslap.
mysqlslap是一個負載仿真客戶端,用作MySQL的負載測試工具。 它可以使用自動生成的查詢或在數據庫上執行的某些自定義查詢來測試MySQL服務器。 它是隨MySQL客戶端軟件包一起安裝的,因此您無需安裝它。 取而代之的是,您將下載一個僅用于測試目的的數據庫,您可以在該數據庫上使用mysqlslap 。
In this tutorial, you will use a sample employee database. You will be using this employee database because it features a large data set that can illustrate differences in query optimization. The database has six tables, but the data it contains has more than 300,000 employee records. This will help you emulate a large-scale production workload.
在本教程中,您將使用示例雇員數據庫 。 您將使用此員工數據庫,因為它具有大型數據集,可以說明查詢優化中的差異。 該數據庫有六個表,但其中包含的數據具有300,000多條員工記錄。 這將幫助您模擬大規模生產工作負載。
To download the database, first clone the Github repository using this command:
要下載數據庫,請首先使用以下命令克隆Github存儲庫:
- git clone https://github.com/datacharmer/test_db.git git clone https://github.com/datacharmer/test_db.git
Then enter the test_db directory and load the database into the MySQL server using these commands:
然后進入test_db目錄,并使用以下命令將數據庫加載到MySQL服務器中:
- cd test_db cd test_db
- mysql -uroot -p < employees.sql mysql -uroot -p <員工.sql
This command uses shell redirection to read the SQL queries in employees.sql file and execute them on the MySQL server to create the database structure.
此命令使用外殼重定向來讀取employees.sql文件中SQL查詢,并在MySQL服務器上執行它們以創建數據庫結構。
You will see output like this:
您將看到如下輸出:
OutputINFO CREATING DATABASE STRUCTURE INFO storage engine: InnoDB INFO LOADING departments INFO LOADING employees INFO LOADING dept_emp INFO LOADING dept_manager INFO LOADING titles INFO LOADING salaries data_load_time_diff 00:00:32Once the database is loaded into your MySQL server, test that mysqlslap is working with the following query:
將數據庫加載到您MySQL服務器后,測試mysqlslap是否可以使用以下查詢:
- mysqlslap -usammy -p -P6033 -h127.0.0.1 --auto-generate-sql --verbose mysqlslap -usammy -p -P6033 -h127.0.0.1 --auto-generate-sql --verbose
mysqlslap has similar flags to the mysql client; here are the ones used in this command:
mysqlslap具有與mysql客戶端相似的標志; 這是此命令中使用的那些:
-u specifies the user used to connect to the server.
-u指定用于連接服務器的用戶。
-p prompts for the user’s password.
-p提示輸入用戶密碼。
-P connects using the specified port.
-P使用指定的端口連接。
-h connects to the specified host.
-h連接到指定的主機。
--auto-generate-sql lets MySQL perform load testing using its own generated queries.
--auto-generate-sql允許MySQL使用其自身生成的查詢執行負載測試。
--verbose makes the output show more information.
--verbose使輸出顯示更多信息。
You will get output similar to the following:
您將獲得類似于以下內容的輸出:
OutputBenchmarkAverage number of seconds to run all queries: 0.015 secondsMinimum number of seconds to run all queries: 0.015 secondsMaximum number of seconds to run all queries: 0.015 secondsNumber of clients running queries: 1Average number of queries per client: 0In this output, you can see the average, minimum, and maximum number of seconds spent to execute all queries. This gives you an indication about the amount of time needed to execute the queries by a number of clients. In this output, only one client was used to execute queries.
在此輸出中,您可以看到執行所有查詢所花費的平均,最小和最大秒數。 這為您指示了許多客戶端執行查詢所需的時間。 在此輸出中,僅使用一個客戶端來執行查詢。
Next, find out what queries mysqlslap executed in the last command by looking at ProxySQL’s stats_mysql_query_digest. This will give us information like the digest of the queries, which is a normalized form of the SQL statement that can be referenced later to enable caching.
接下來,通過查看ProxySQL的stats_mysql_query_digest上一條命令中對mysqlslap執行的stats_mysql_query_digest 。 這將為我們提供諸如查詢摘要之類的信息,這是SQL語句的規范化形式,以后可以引用該形式以啟用緩存。
Enter the ProxySQL admin interface with this command:
使用以下命令輸入ProxySQL管理界面:
- mysql -uadmin -p -h 127.0.0.1 -P6032 mysql -uadmin -p -h 127.0.0.1 -P6032
Then execute this query to find information in the stats_mysql_query_digest table:
然后執行此查詢以在stats_mysql_query_digest表中查找信息:
- SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC; 從stats_mysql_query_digest中選擇count_star,sum_time,hostgroup,digest,digest_text ORDER BY sum_time DESC;
You will see output similar to the following:
您將看到類似于以下內容的輸出:
+------------+----------+-----------+--------------------+----------------------------------+ | count_star | sum_time | hostgroup | digest | digest_text | +------------+----------+-----------+--------------------+----------------------------------+ | 1 | 598 | 1 | 0xF8F780C47A8D1D82 | SELECT @@HOSTNAME as hostname | | 1 | 0 | 1 | 0x226CD90D52A2BA0B | select @@version_comment limit ? | +------------+----------+-----------+--------------------+----------------------------------+ 2 rows in set (0.01 sec)The previous query selects data from the stats_mysql_query_digest table, which contains information about all executed queries in ProxySQL. Here you have five columns selected:
上一個查詢從stats_mysql_query_digest表中選擇數據,該表包含有關ProxySQL中所有已執行查詢的信息。 在這里,您選擇了五列:
count_star: The number of times this query was executed.
count_star :執行此查詢的次數。
sum_time: Total time in milliseconds that this query took to execute.
sum_time :此查詢執行的總時間(以毫秒為單位)。
hostgroup: The hostgroup used to execute the query.
hostgroup :用于執行查詢的主機組。
digest: A digest of the executed query.
digest :已執行查詢的摘要。
digest_text: The actual query. In this tutorial’s example, the second query is parameterized using ? marks in place of variable parameters. select @@version_comment limit 1 and select @@version_comment limit 2, therefore, are grouped together as the same query with the same digest.
digest_text :實際查詢。 在本教程的示例中,第二個查詢使用?參數化? 標記代替可變參數。 select @@version_comment limit 1和select @@version_comment limit 2 ,因此將它們組合為具有相同摘要的同一查詢。
Now that you know how to check query data in the stats_mysql_query_digest table, exit the mysql shell:
現在,您知道如何檢查stats_mysql_query_digest表中的查詢數據,退出mysql shell:
- exit; 出口;
The database you downloaded contains some tables with demo data. You will now test queries on the dept_emp table by selecting any records whose from_date is greater than 2000-04-20 and recording the average execution time.
您下載的數據庫包含一些帶有演示數據的表。 現在,通過選擇from_date大于2000-04-20任何記錄并記錄平均執行時間,來測試dept_emp表上的查詢。
Use this command to run the test:
使用以下命令運行測試:
- mysqlslap -usammy -P6033 -p -h127.0.0.1 --concurrency=100 --iterations=20 --create-schema=employees --query="SELECT * from dept_emp WHERE from_date>'2000-04-20'" --verbose mysqlslap -usammy -P6033 -p -h127.0.0.1 --concurrency = 100 --iterations = 20 --create-schema = employees --query =“ SELECT * from dept_emp WHERE from_date>'2000-04-20'” -詳細
Here you are using some new flags:
在這里,您使用了一些新的標志:
--concurrency=100: This sets the number of users to simulate, in this case 100.
--concurrency=100 :設置要模擬的用戶數,在這種情況下為100 。
--iterations=20: This causes the test to run 20 times and calculate results from all of them.
--iterations=20 :這將導致測試運行20次并從所有計算結果。
--create-schema=employees: Here you selected the employees database.
--create-schema=employees :在這里您選擇了employees數據庫。
--query="SELECT * from dept_emp WHERE from_date>'2000-04-20'": Here you specified the query executed in the test.
--query="SELECT * from dept_emp WHERE from_date>'2000-04-20'" :在此處,您指定了在測試中執行的查詢。
The test will take a few minutes. After it is done, you will get results similar to the following:
測試將需要幾分鐘。 完成后,您將獲得類似于以下內容的結果:
OutputBenchmarkAverage number of seconds to run all queries: 18.117 secondsMinimum number of seconds to run all queries: 8.726 secondsMaximum number of seconds to run all queries: 22.697 secondsNumber of clients running queries: 100Average number of queries per client: 1Your numbers could be a little different. Keep these numbers somewhere in order to compare them with the results from after you enable caching.
您的電話號碼可能會有所不同。 將這些數字保留在某處,以便將它們與啟用緩存后的結果進行比較。
After testing ProxySQL without caching, it is time to run the same test again, but this time with caching enabled.
在不緩存的情況下測試ProxySQL之后,是時候再次運行相同的測試了,但是這次啟用了緩存。
第4步-使用mysqlslap和緩存進行測試 (Step 4 — Testing Using mysqlslap With Caching)
In this step, caching will help us to decrease latency when executing similar queries. Here, you will identify the queries executed, take their digests from ProxySQL’s stats_mysql_query_digest table, and use them to enable caching. Then, you will test again to check the difference.
在這一步中,緩存將幫助我們減少執行類似查詢時的延遲。 在這里,您將確定執行的查詢,從ProxySQL的stats_mysql_query_digest表中提取其摘要,并使用它們來啟用緩存。 然后,您將再次測試以檢查差異。
To enable caching, you need to know the digests of the queries that will be cached. Log in to the ProxySQL admin interface using this command:
要啟用緩存,您需要知道將要緩存的查詢的摘要。 使用以下命令登錄到ProxySQL管理界面:
- mysql -uadmin -p -h127.0.0.1 -P6032 mysql -uadmin -p -h127.0.0.1 -P6032
Then execute this query again to get a list of queries executed and their digests:
然后再次執行此查詢以獲取已執行查詢及其摘要的列表:
- SELECT count_star,sum_time,hostgroup,digest,digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC; 從stats_mysql_query_digest中選擇count_star,sum_time,hostgroup,digest,digest_text ORDER BY sum_time DESC;
You will get a result similar to this:
您將得到類似于以下結果:
Output+------------+-------------+-----------+--------------------+------------------------------------------+ | count_star | sum_time | hostgroup | digest | digest_text | +------------+-------------+-----------+--------------------+------------------------------------------+ | 2000 | 33727110501 | 1 | 0xC5DDECD7E966A6C4 | SELECT * from dept_emp WHERE from_date>? | | 1 | 601 | 1 | 0xF8F780C47A8D1D82 | SELECT @@HOSTNAME as hostname | | 1 | 0 | 1 | 0x226CD90D52A2BA0B | select @@version_comment limit ? | +------------+-------------+-----------+--------------------+------------------------------------------+ 3 rows in set (0.00 sec)Look at the first row. It is about a query that was executed 2000 times. This is the benchmarked query executed previously. Take its digest and save it to be used in adding a query rule for caching.
看第一行。 它是關于一個已執行2000次的查詢。 這是先前執行的基準查詢。 提取摘要并保存,以添加用于緩存的查詢規則。
The next few queries will add a new query rule to ProxySQL that will match the digest of the previous query and put a cache_ttl value for it. cache_ttl is the number of milliseconds that the result will be cached in memory:
接下來的幾個查詢將向ProxySQL添加一個新查詢規則,該規則將與先前查詢的摘要匹配,并為其添加一個cache_ttl值。 cache_ttl是結果將被緩存在內存中的毫秒數:
INSERT INTO mysql_query_rules(active, digest, cache_ttl, apply) VALUES(1,'0xC5DDECD7E966A6C4',2000,1);
插入mysql_query_rules(活動,摘要,cache_ttl,應用)VALUES(1,' 0xC5DDECD7E966A6C4 ',2000,1);
In this command you are adding a new record to the mysql_query_rules table; this table holds all the rules applied before executing a query. In this example, you are adding a value for the cache_ttl column that will cause the matched query by the given digest to be cached for a number of milliseconds specified in this column. You put 1 in the apply column to make sure that the rule is applied to queries.
在此命令中,您將新記錄添加到mysql_query_rules表中; 該表包含執行查詢之前應用的所有規則。 在此示例中,您要為cache_ttl列添加一個值,該值將使給定摘要的匹配查詢被緩存此列中指定的毫秒數。 您在“應用”列中輸入1以確保將規則應用于查詢。
LOAD and SAVE these changes, then exit the mysql shell:
LOAD并SAVE這些更改,然后退出mysql shell:
- LOAD MYSQL QUERY RULES TO RUNTIME; 將MYSQL查詢規則加載到運行時;
- SAVE MYSQL QUERY RULES TO DISK; 保存MYSQL查詢規則到磁盤;
- exit; 出口;
Now that caching is enabled, re-run the test again to check the result:
現在已啟用緩存,請再次重新運行測試以檢查結果:
- mysqlslap -usammy -P6033 -p -h127.0.0.1 --concurrency=100 --iterations=20 --create-schema=employees --query="SELECT * from dept_emp WHERE from_date>'2000-04-20'" --verbose mysqlslap -usammy -P6033 -p -h127.0.0.1 --concurrency = 100 --iterations = 20 --create-schema = employees --query =“ SELECT * from dept_emp WHERE from_date>'2000-04-20'” -詳細
This will give output similar to the following:
這將產生類似于以下內容的輸出:
OutputBenchmarkAverage number of seconds to run all queries: 7.020 secondsMinimum number of seconds to run all queries: 0.274 secondsMaximum number of seconds to run all queries: 23.014 secondsNumber of clients running queries: 100Average number of queries per client: 1Here you can see the big difference in average execution time: it dropped from 18.117 seconds to 7.020.
在這里,您可以看到平均執行時間的巨大差異:它從18.117秒降低到7.020 。
結論 (Conclusion)
In this article, you set up transparent caching with ProxySQL to cache database query results. You also tested the query speed with and without caching to see the difference that caching can make.
在本文中,您將使用ProxySQL設置透明緩存來緩存數據庫查詢結果。 您還測試了使用和不使用緩存的查詢速度,以了解緩存可以帶來的不同。
You’ve used one level of caching in this tutorial. You could also try, web caching, which sits in front of a web server and caches the responses to similar requests, sending the response back to the client without hitting the back-end servers. This is very similar to ProxySQL caching but at a different level. To learn more about web caching, check out our Web Caching Basics: Terminology, HTTP Headers, and Caching Strategies primer.
在本教程中,您已經使用了一級緩存。 您還可以嘗試Web緩存 ,它位于Web服務器的前面,并緩存對類似請求的響應,將響應發送回客戶端而不打到后端服務器。 這與ProxySQL緩存非常相似,但級別不同。 要了解有關Web緩存的更多信息,請查看我們的Web緩存基礎知識:術語,HTTP標頭和緩存策略入門 。
MySQL server also has its own query cache; you can learn more about it in our How To Optimize MySQL with Query Cache on Ubuntu 18.04 tutorial.
MySQL服務器也有自己的查詢緩存; 您可以在《 如何在Ubuntu 18.04上使用查詢緩存優化MySQL》中了解有關它的更多信息。
翻譯自: https://www.digitalocean.com/community/tutorials/how-to-optimize-mysql-queries-with-proxysql-caching-on-ubuntu-16-04
總結
以上是生活随笔為你收集整理的如何在Ubuntu 16.04上使用ProxySQL缓存优化MySQL查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年1月语音合成(TTS)和语音识
- 下一篇: 《童虎学习笔记》14分钟结合ProxyS