当前位置:网站首页>TiKV & TiFlash accelerate complex business queries丨TiFlash application practice
TiKV & TiFlash accelerate complex business queries丨TiFlash application practice
2022-08-03 16:38:00 【TiDB_PingCAP】
Complex business queries are a test for traditional relational databases,而通过 TiKV row storage and TiFlash The combination of column storage can be well dealt with.本文根据 TUG User Biancheng Yuanyuan is here TiDB The sharing and sorting of the Shijiazhuang Station of the Community Technology Exchange,详细介绍了 TiKV & TiFlash Principles and practical solutions for accelerating complex business queries.
背景
In Internet companies or traditional companies CRM 系统中,One of the most frequently used functions is customer filtering.通过不同的角度、维度、A combination of tags to box select customers,for subsequent business operations.
This is undoubtedly the traditional relational database,Or the architecture of relational database plus column storage database is a test,There are mainly the following pain points:
Traditional relational databases cannot optimize and speed up queries by adding indexes,业务无法正常开展;
Column-stored databases need to put filter-related data into the column database,And need to do real-time synchronization of data;
Data cannot be read from the database level,It is often necessary to read data from a column database and then to a relational database for data merging and output,Performance is not optimistic.
TiDB 数据库的 TiKV 和 TiFlash The combination of theoretically solves several of the above pain points.
TiKV 行存 与 TiFlash 列存混合使用
TiDB 中 query 执行的示意图,可以看到在 TiDB 中一个 query 的执行会被分成两部分,一部分在 TiDB 执行,一部分下推给存储层( TiFlash/TiKV )执行.

混用原理
TiDB 的行列混合并不是传统设计上的行存列存二选一,而是 TiDB 可以在同一张表同时拥有行存和列存,且两者永远保持数据强一致(而非最终一致).
多表查询分别使用不同的引擎 TiKV 或 TiFlash.
TiFlash 支持 MPP 模式的查询执行,即在计算中引入跨节点的数据交换(data shuffle 过程).
混用优化

标签系统高级筛选
通过标签(从宽表里不确定字段)和窄表特定字段组合查询客户并分页

Read from TiKV
SELECT
/*+ READ_FROM_STORAGE(tikv[b], tikv[c],tikv[d]) */
a.*,
b.CUST_NAME,b.CERT_TYPE,b.CERT_NUM,b.CUST_TYPE,b.SEX,b.AGE,b.BIRTH_DT,
c.ORG_ID,c.ORG_NAME,
d.ASSET,d.ASSET_MON_AVG
FROM
(
SELECT /*+ READ_FROM_STORAGE(tikv[m],tikv[n]) */
m.cust_id
FROM
m_cust_label m
RIGHT JOIN m_cust_org n ON m.CUST_ID = n.CUST_ID
WHERE
m.cat1 IN ( 516, 710, 230,3301 )
AND n.ORG_ID IN ( '133','8716', '7162') ORDER BY n.cust_id ASC LIMIT 100
) a
LEFT JOIN m_cust_main b ON a.cust_id = b.cust_id
LEFT JOIN m_cust_org c ON a.cust_id = c.cust_id
LEFT JOIN m_cust_data d ON a.cust_id = d.cust_id ;
4G,2c 虚拟机 300 万数据,首次执行 48 s 二次执行 0.7s
Read From TiKV & TiFlash
SELECT
/*+ READ_FROM_STORAGE(tikv[b], tikv[c],tikv[d]) */
a.*,
b.CUST_NAME,b.CERT_TYPE,b.CERT_NUM,b.CUST_TYPE,b.SEX,b.AGE,b.BIRTH_DT,
c.ORG_ID,c.ORG_NAME,
d.ASSET,d.ASSET_MON_AVG
FROM
(
SELECT /*+ READ_FROM_STORAGE(tiflash[m],tikv[n]) */
m.cust_id
FROM
m_cust_label m
RIGHT JOIN m_cust_org n ON m.CUST_ID = n.CUST_ID
WHERE
m.cat1 IN ( 516, 710, 230,3301 )
AND n.ORG_ID IN ( '133','8716', '7162') ORDER BY n.cust_id ASC LIMIT 100
) a
LEFT JOIN m_cust_main b ON a.cust_id = b.cust_id
LEFT JOIN m_cust_org c ON a.cust_id = c.cust_id
LEFT JOIN m_cust_data d ON a.cust_id = d.cust_id
4G,2c 虚拟机 300 万数据,首次执行 3s 二次执行 0.3s
TiFlash & MPP
Controls whether to select MPP 模式
变量 tidb_allow_mpp 控制 TiDB 能否选择 MPP 模式执行查询.变量 tidb_enforce_mpp Controls whether to ignore optimizer cost estimates,强制使用 TiFlash 的 MPP 模式执行查询.
The results corresponding to all the values of these two variables are as follows:
| tidb_allow_mpp=off | tidb_allow_mpp=on(默认) | |
|---|---|---|
| tidb_enforce_mpp=off(默认) | 不使用 MPP 模式. | The optimizer is chosen based on the cost estimate.(默认) |
| tidb_enforce_mpp=on | 不使用 MPP 模式. | TiDB 无视代价估算,选择 MPP 模式. |
set @@session.tidb_allow_mpp=1;
set @@session.tidb_enforce_mpp=1;
SELECT
/*+ READ_FROM_STORAGE(tikv[b], tikv[c],tikv[d]) */
a.*,
b.CUST_NAME,b.CERT_TYPE,b.CERT_NUM,b.CUST_TYPE,b.SEX,b.AGE,b.BIRTH_DT,
c.ORG_ID,c.ORG_NAME,
d.ASSET,d.ASSET_MON_AVG
FROM
(
SELECT /*+ READ_FROM_STORAGE(tiflash[m],tiflash[n]) */
m.cust_id
FROM
m_cust_label m
RIGHT JOIN m_cust_org n ON m.CUST_ID = n.CUST_ID
WHERE
m.cat1 IN ( 516, 710, 230,3301 )
AND n.ORG_ID IN ( '133','8716', '7162') ORDER BY n.cust_id ASC LIMIT 100
) a
LEFT JOIN m_cust_main b ON a.cust_id = b.cust_id
LEFT JOIN m_cust_org c ON a.cust_id = c.cust_id
LEFT JOIN m_cust_data d ON a.cust_id = d.cust_id
使用 MPP 模式来执行查询后基本秒开,4G 2c 虚拟机 300 万数据,首次执行 1s 二次执行 0.15s
2.4 SPM 固定执行计划
CREATE GLOBAL|SESSION BINDING for <BindableStmt > USING <BindableStmt2>
SHOW GLOBAL|SESSION BINDINGS ; -- 查看绑定计划explain format = 'verbose' <BindableStmt2>;
show warnings; -- 通过执行 show warnings 了解该 SQL 语句使用了哪一条 binding
固定特定查询走 TiFlash 列存查询.

标签下价值机构排名
根据选中的属性(多值)
使用这些值最多的排名前 3 的机构,并统计出总额

执行计划
table:c 走 TiFlash ;table:a, table:b 走 TiKV ,同时使用了列存和行存的优势.



总结
使用 TiKV 和 TiFlash 可以加速复杂查询,下面简单增加了使用使用场景.
| 组件 | 适用场景说明 |
|---|---|
| TiKV | 检索条件固定,且有索引 |
| TiFlash | 检索条件不固定,无法加索引 |
| TiKV + TiFlash | 部分表检索条件不固定,部分表有索引 |
如果有描述不当的地方欢迎评论指正!
谢谢 PingCAP 社区的大力支持!
边栏推荐
猜你喜欢

#夏日挑战赛# HarmonyOS 实现一个绘画板

C专家编程 第1章 C:穿越时空的迷雾 1.9 阅读ANSI C标准,寻找乐趣和裨益

《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架

高效的组织信息共享知识库是一种宝贵的资源

一文看懂推荐系统:概要02:推荐系统的链路,从召回粗排,到精排,到重排,最终推荐展示给用户

Yuan xiaolin: Volvo focus on travel security, and put it perfectly

MATLAB | 一种简易的随机曼陀罗图形生成函数
![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 02](/img/45/96af4ca21329964808a4c8f2b8272c.png)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 02

Introduction to spark learning - 1

甲方不让用开源【监控软件】?大不了我自己写一个
随机推荐
我写了个”不贪吃蛇“小游戏
面试不再被吊打!这才是Redis分布式锁的七种方案的正确打开方式
MySQL窗口函数
When mobile applications go overseas, is your "network optimization" holding back?
leetcode SVM
Detailed explanation of ReentrantReadWriteLock
Kubernetes 笔记 / 任务 / 管理集群 / 用 kubeadm 管理集群 / 配置一个 cgroup 驱动
devops-2:Jenkins的使用及Pipeline语法讲解
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 安装 kubeadm
WordPress 5.2.3 更新,升级出现请求超时的解决方法
QT QT 】 【 to have developed a good program for packaging into a dynamic library
如何设计大电流九线导电滑环
不可忽略!户外LED显示屏的特点及优势
WordPress建站技术笔记
为什么我强烈推荐使用智能化async?
如何在 DataWorks 中 写SQL语句监控数据的变化到达一定的值 进行提示
兄弟组件通信context
To participate in sweepstakes, incoming new programmers magazine welfare!
13 and OOM simulation
Kubernetes 笔记 / 生产环境