由于某种原因,ProxySQL 查询缓存并不总是遵守查询规则
Posted
技术标签:
【中文标题】由于某种原因,ProxySQL 查询缓存并不总是遵守查询规则【英文标题】:ProxySQL Query Cache doesn't always respect the query rules for some reason 【发布时间】:2021-08-09 02:51:13 【问题描述】:我使用 ProxySQL (2.0.17) 缓存所有发送到 mysql 的 SELECT 查询。 mysql_query_rules
表如下所示:
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+------------------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest | match_pattern | negate_match_pattern | re_modifiers | flagOUT | replace_pattern | destination_hostgroup | cache_ttl | cache_empty_result | cache_timeout | reconnect | timeout | retries | delay | next_query_flagIN | mirror_flagOUT | mirror_hostgroup | error_msg | OK_msg | sticky_conn | multiplex | gtid_from_hostgroup | log | apply | comment |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+------------------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
| 1 | 1 | NULL | NULL | 0 | NULL | NULL | NULL | NULL | ^[(]?SELECT (?!SQL_NO_CACHE) | NULL | 0 | CASELESS | NULL | NULL | NULL | 300000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | NULL |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+------------------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
一个简单的规则(我也尝试过^SELECT .*
)和 300 秒等待缓存的查询被清除。
由于某种原因,每个要缓存的查询的 5% 仍会发送到后端。例如,这是最流行的查询:
+-----------+------------+----------+----------------+--------------------+--------------------------+------------+------------+------------+-------------+----------+----------+-------------------+---------------+
| hostgroup | schemaname | username | client_address | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time | sum_rows_affected | sum_rows_sent |
+-----------+------------+----------+----------------+--------------------+--------------------------+------------+------------+------------+-------------+----------+----------+-------------------+---------------+
| 2 | ------ | ---- | | 0xFB50749BCFE0DA3C | SELECT * FROM `language` | 12839 | 1621445210 | 1621455115 | 45069293213 | 31321 | 82235606 | 0 | 56960 |
| -1 | ------ | ---- | | 0xFB50749BCFE0DA3C | SELECT * FROM `language` | 326243 | 1621445210 | 1621455116 | 0 | 0 | 0 | 0 | 0 |
+-----------+------------+----------+----------------+--------------------+--------------------------+------------+------------+------------+-------------+----------+----------+-------------------+---------------+
我无法理解这种特殊性。每当我更新stats_mysql_query_digest
时,hostgroup 2
(后端)上的count_star
都会递增,而无需等待 300 秒以清除查询。
查询缓存大小设置为 512 Mb。在高峰期,它占用大约 100 Mb。
帮助?..
【问题讨论】:
第二个看起来实际上已经兑现 @nbk 这是相同的查询,具有相同的摘要哈希 是的,我已经看到了,但是看看结尾 sum_rows_sent 0,所以不要发送任何行 @nbk "sum_rows_sent: 发送的总行数。这目前不计算从查询缓存返回的行数" 所以它可以为 0 【参考方案1】:将mysql-query_cache_size_MB
增加到 5120 MB(这当然是荒谬的)似乎在一定程度上解决了这个问题。该查询的后端请求频率下降了 10 倍(感谢 ProxySQL 的Query Logging,您可以只记录一个查询并对其进行分析)。 cache_ttl
值仍然远未得到尊重,但我想这种解决方法在这一点上总比没有好。
【讨论】:
以上是关于由于某种原因,ProxySQL 查询缓存并不总是遵守查询规则的主要内容,如果未能解决你的问题,请参考以下文章