SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1;
import time
def get_server_metrics():
try:
conn = pymysql.connect(host='127.0.0.1', user='monitor', port=2881, password='*****', database='oceanbase')
cursor = conn.cursor()
cursor.execute(f"""
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1;
""")
result = cursor.fetchone()
print("result:", result)
return result
except Exception as e:
print("Error:", e)
return (0, 0)
metrics1 = get_server_metrics()
time.sleep(15) # 缩短时间间隔
metrics2 = get_server_metrics()
sys_qpsdiff = (metrics2[1] - metrics1[1]) / 15 # 调整时间间隔
3. 内部查询的影响
自定义脚本:您当前的脚本可能没有排除内部查询,这些查询可能会增加 QPS 的统计值。
官方 Prometheus + Grafana 模板:通常会排除内部查询,以确保统计结果的准确性。
优化建议:
排除内部查询:您可以尝试排除内部查询,例如使用以下 SQL 语句:
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1 AND name NOT LIKE '%internal%';
import time
from collections import deque
window_size = 5 # 平滑窗口大小
qps_values = deque(maxlen=window_size)
def get_server_metrics():
try:
conn = pymysql.connect(host='127.0.0.1', user='monitor', port=2881, password='*****', database='oceanbase')
cursor = conn.cursor()
cursor.execute(f"""
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1 AND name NOT LIKE '%internal%';
""")
result = cursor.fetchone()
print("result:", result)
return result
except Exception as e:
print("Error:", e)
return (0, 0)
while True:
metrics1 = get_server_metrics()
time.sleep(15) # 缩短时间间隔
metrics2 = get_server_metrics()
qps_diff = (metrics2[1] - metrics1[1]) / 15 # 调整时间间隔
qps_values.append(qps_diff)
smoothed_qps = sum(qps_values) / len(qps_values)
print("Smoothed QPS:", smoothed_qps)
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1;
import time
def get_server_metrics():
try:
conn = pymysql.connect(host='127.0.0.1', user='monitor', port=2881, password='*****', database='oceanbase')
cursor = conn.cursor()
cursor.execute(f"""
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1;
""")
result = cursor.fetchone()
print("result:", result)
return result
except Exception as e:
print("Error:", e)
return (0, 0)
metrics1 = get_server_metrics()
time.sleep(15) # 缩短时间间隔
metrics2 = get_server_metrics()
sys_qpsdiff = (metrics2[1] - metrics1[1]) / 15 # 调整时间间隔
3. 内部查询的影响
阿里脚本:可能排除了内部查询,以确保统计结果的准确性。
自定义脚本:您当前的脚本可能没有排除内部查询,这些查询可能会增加 QPS 的统计值。
优化建议:
排除内部查询:您可以尝试排除内部查询,例如使用以下 SQL 语句:
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1 AND name NOT LIKE '%internal%';
4. 数据平滑处理
阿里脚本:可能进行了数据平滑处理,以减少突发查询的影响。
自定义脚本:您当前的脚本没有进行数据平滑处理,可能会受到突发查询的影响。
优化建议:
数据平滑处理:您可以尝试对数据进行平滑处理,例如使用移动平均法。以下是一个简单的示例:
import time
from collections import deque
window_size = 5 # 平滑窗口大小
qps_values = deque(maxlen=window_size)
def get_server_metrics():
try:
conn = pymysql.connect(host='127.0.0.1', user='monitor', port=2881, password='*****', database='oceanbase')
cursor = conn.cursor()
cursor.execute(f"""
SELECT
SUM(CASE WHEN name = 'queries on user tables' THEN value ELSE 0 END) AS select_qps,
SUM(CASE WHEN name = 'DML statements executed' THEN value ELSE 0 END) AS dml_qps
FROM v$sysstat
WHERE class < 1000 AND con_id = 1 AND name NOT LIKE '%internal%';
""")
result = cursor.fetchone()
print("result:", result)
return result
except Exception as e:
print("Error:", e)
return (0, 0)
while True:
metrics1 = get_server_metrics()
time.sleep(15) # 缩短时间间隔
metrics2 = get_server_metrics()
qps_diff = (metrics2[1] - metrics1[1]) / 15 # 调整时间间隔
qps_values.append(qps_diff)
smoothed_qps = sum(qps_values) / len(qps_values)
print("Smoothed QPS:", smoothed_qps)