[转帖]043、TiDB特性_缓存表和分区表

tidb,特性,缓存,分区表 · 浏览次数 : 0

小编点评

排版方面,我建议您使用以下方法: 1. 使用 `print` 方法在 `set` 中打印元素。 2. 使用 `set` 的 ``join`` 方法将元素打印到字符串中。 3. 使用 ``format`` 方法格式化字符串,使用元素和格式符。 4. 使用 ``join`` 方法将元素打印到 ``print`` 中。 例如,以下代码展示了如何使用 `print` 和 ``join`` 方法打印元素: ```python print("set:", {"1", "2", "3"}) print("set:", {"1", "2", "3"}.join("-")) print("set:", {"1", "2", "3"}.format("-")) print("set:", {"1", "2", "3"}.join("-")) ``` 输出如下: ``` set: {"1", "2", "3"} set: -1 set: -1 set: -1 ``` 希望这些方法能够帮助您完成排版操作。

正文

针对于优化器在索引存在时依然使⽤全表扫描的情况下,使⽤缓存表和分区表是提升查询性能的有效⼿段。
在这里插入图片描述

缓存表

  • 缓存表是将表的内容完全缓存到 TiDB Server 的内存中
  • 表的数据量不⼤,⼏乎不更改
  • 读取很频繁
  • 缓存控制: ALTER TABLE table_name CACHE|NOCACHE;
# 使用trace跟踪下
tidb> TRACE SELECT * FROM test.c1;
+-------------------------------------------+-----------------+---
---------+
| operation | startTS |
duration |
+-------------------------------------------+-----------------+---
---------+
| trace | 18:39:25.485266 |
501.582µs |
| ├─session.ExecuteStmt | 18:39:25.485270 |
432.208µs |
| │ ├─executor.Compile | 18:39:25.485281 |
132.616µs |
| │ └─session.runStmt | 18:39:25.485433 |
249.488µs |
| │ └─UnionScanExec.Open | 18:39:25.485572 |
72.776µs |
| │ ├─TableReaderExecutor.Open | 18:39:25.485575 |
13.24µs |
| │ ├─buildMemTableReader | 18:39:25.485605 |
3.283µs |
| │ └─memTableReader.getMemRows | 18:39:25.485615 |   # memTableReader.getMemRows 表示从缓存取数
20.558µs |
| ├─*executor.ProjectionExec.Next | 18:39:25.485712 |
12.911µs |
| │ └─*executor.UnionScanExec.Next | 18:39:25.485714 |
3.823µs |
| └─*executor.ProjectionExec.Next | 18:39:25.485733 |
8.943µs |
| └─*executor.UnionScanExec.Next | 18:39:25.485735 |
1.33µs |
+-------------------------------------------+-----------------+---
---------+
12 rows in set (0.00 sec)

    小表缓存-原理

    在这里插入图片描述

    缓存租约

    • 租约时间内,无法进行写操作
      在这里插入图片描述

    • 租约到期,数据过期

    • 写操作不再被阻塞

    • 读写直接到TiKV节点上执行
      在这里插入图片描述

    • 数据更新完毕,租约继续开启

    在这里插入图片描述

    应用场景

    • 每张缓存表的大小限制为64MB
    • 适用于查询频繁、数据量不大、修改极少的场景
    • 在租约(tidb_table_cache_lease) 时间内,写操作会被阻塞
    • 当租约到期(tidb_table_cache_lease)时,读性能会下降
    • 不支持对缓存表直接做DDL操作,需要先关闭
    • 对于表加载较慢或者极少修改的表,可以适当延长tidb_table_cache_lease保持读性能稳定

    分区表

    分区类型与适用场景

    • range: 分区剪裁,节省IO开销
    • Hash: 用于大规模写入的情况下将数据打散,平均地分配到各个分区里
    Range分区表
    create table t1(x int) partition by name(x) (
    partition p0 values less than(5),
    partition p1 values less than (10));
    )
    

      分区类型与适⽤场景

      • Range

      分区裁剪, 节省 I/O 开销

      /* Range Partition t1 */
      drop table if exists test.t1;
      create table test.t1 (x int) partition by range (x) (
       partition p0 values less than (5),
       partition p1 values less than (10));
      insert into test.t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      insert into test.t1 select * from test.t1;
      /* Check Partition Pruning */
      explain select * from test.t1 where x between 1 and 4;
      
        查看执行计划
        mysql> explain select * from test.t1 where x between 1 and 4;
        +-------------------------+-----------+-----------+------------------------+------------------------------------+
        | id                      | estRows   | task      | access object          | operator info                      |
        +-------------------------+-----------+-----------+------------------------+------------------------------------+
        | TableReader_9           | 12288.00  | root      |                        | data:Selection_8                   |
        | └─Selection_8           | 12288.00  | cop[tikv] |                        | ge(test.t1.x, 1), le(test.t1.x, 4) |
        |   └─TableFullScan_7     | 491520.00 | cop[tikv] | table:t1, partition:p0 | keep order:false                   |
        +-------------------------+-----------+-----------+------------------------+------------------------------------+
        3 rows in set (0.03 sec)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        /* Check regions */
        show table test.t1 regions;
        mysql> show table test.t1 regions;
        +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
        | REGION_ID | START_KEY | END_KEY | LEADER_ID | LEADER_STORE_ID | PEERS | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS |
        +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
        |      5019 | t_79_     | t_80_   |      5020 |            1001 | 5020  |          0 |             0 |          0 |                   67 |           877551 |
        |      1002 | t_80_     |         |      1003 |            1001 | 1003  |          0 |             0 |          0 |                   24 |           421921 |
        +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
        2 rows in set (0.02 sec)
        
          • Hash

          可以⽤于⼤规模写⼊的情况下将数据打散, 平均地分配到各个分区⾥

          /* Hash Partition t1 */
          drop table if exists test.t1;
          CREATE TABLE test.t1 (x INT)
           PARTITION BY HASH(x)
           PARTITIONS 4;
          

          insert into test.t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;
          insert into test.t1 select * from test.t1;

            查看分区的执行计划

            • 默认是通过mod方式分配分区
            /* Check Partition Distribution */
            

            explain select from test.t1 where x=0;
            explain select
            from test.t1 where x=1;
            explain select from test.t1 where x=2;
            explain select
            from test.t1 where x=3;
            explain select from test.t1 where x=4;
            explain select
            from test.t1 where x=5;
            explain select from test.t1 where x=6;
            explain select
            from test.t1 where x=7;
            explain select from test.t1 where x=8;
            explain select
            from test.t1 where x=9;
            /* Negative /
            explain select
            from test.t1 where x between 7 and 9;
            mysql> explain select * from test.t1 where x between 7 and 9;
            +------------------------------+----------+-----------+------------------------+------------------------------------+
            | id | estRows | task | access object | operator info |
            +------------------------------+----------+-----------+------------------------+------------------------------------+
            | PartitionUnion_10 | 750.00 | root | | |
            | ├─TableReader_13 | 250.00 | root | | data:Selection_12 |
            | │ └─Selection_12 | 250.00 | cop[tikv] | | ge(test.t1.x, 7), le(test.t1.x, 9) |
            | │ └─TableFullScan_11 | 10000.00 | cop[tikv] | table:t1, partition:p0 | keep order:false, stats:pseudo |
            | ├─TableReader_16 | 250.00 | root | | data:Selection_15 |
            | │ └─Selection_15 | 250.00 | cop[tikv] | | ge(test.t1.x, 7), le(test.t1.x, 9) |
            | │ └─TableFullScan_14 | 10000.00 | cop[tikv] | table:t1, partition:p1 | keep order:false, stats:pseudo |
            | └─TableReader_19 | 250.00 | root | | data:Selection_18 |
            | └─Selection_18 | 250.00 | cop[tikv] | | ge(test.t1.x, 7), le(test.t1.x, 9) |
            | └─TableFullScan_17 | 10000.00 | cop[tikv] | table:t1, partition:p3 | keep order:false, stats:pseudo |
            +------------------------------+----------+-----------+------------------------+------------------------------------+
            10 rows in set (12.69 sec)

              查看region分布情况

              /* Check regions */
              # 查看对应的region情况,在4个region上
              mysql> show table test.t1 regions;
              +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
              | REGION_ID | START_KEY | END_KEY | LEADER_ID | LEADER_STORE_ID | PEERS | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS |
              +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
              |      9003 | t_84_     | t_85_   |      9004 |            1001 | 9004  |          0 |      18449249 |   14117466 |                   13 |           141205 |
              |      9005 | t_85_     | t_86_   |      9006 |            1001 | 9006  |          0 |      18443067 |   14024508 |                   10 |            58475 |
              |      9007 | t_86_     | t_87_   |      9008 |            1001 | 9008  |          0 |      12295360 |    8703102 |                    5 |            27228 |
              |      1002 | t_87_     |         |      1003 |            1001 | 1003  |          0 |      12295959 |    9218671 |                    4 |            26666 |
              +-----------+-----------+---------+-----------+-----------------+-------+------------+---------------+------------+----------------------+------------------+
              4 rows in set (2 min 46.43 sec)
              

                与[转帖]043、TiDB特性_缓存表和分区表相似的内容:

                [转帖]043、TiDB特性_缓存表和分区表

                针对于优化器在索引存在时依然使⽤全表扫描的情况下,使⽤缓存表和分区表是提升查询性能的有效⼿段。 缓存表 缓存表是将表的内容完全缓存到 TiDB Server 的内存中表的数据量不⼤,⼏乎不更改读取很频繁缓存控制: ALTER TABLE table_name CACHE|NOCACHE; # 使用t

                [转帖]

                Linux ubuntu20.04 网络配置(图文教程) 因为我是刚装好的最小系统,所以很多东西都没有,在开始配置之前需要做下准备 环境准备 系统:ubuntu20.04网卡:双网卡 网卡一:供连接互联网使用网卡二:供连接内网使用(看情况,如果一张网卡足够,没必要做第二张网卡) 工具: net-to

                [转帖]

                https://cloud.tencent.com/developer/article/2168105?areaSource=104001.13&traceId=zcVNsKTUApF9rNJSkcCbB 前言 Redis作为高性能的内存数据库,在大数据量的情况下也会遇到性能瓶颈,日常开发中只有时刻

                [转帖]ISV 、OSV、 SIG 概念

                ISV 、OSV、 SIG 概念 2022-10-14 12:29530原创大杂烩 本文链接:https://www.cndba.cn/dave/article/108699 1. ISV: Independent Software Vendors “独立软件开发商”,特指专门从事软件的开发、生产、

                [转帖]Redis 7 参数 修改 说明

                2022-06-16 14:491800原创Redis 本文链接:https://www.cndba.cn/dave/article/108066 在之前的博客我们介绍了Redis 7 的安装和配置,如下: Linux 7.8 平台 Redis 7 安装并配置开机自启动 操作手册https://ww

                [转帖]HTTPS中间人攻击原理

                https://www.zhihu.com/people/bei-ji-85/posts 背景 前一段时间,公司北京地区上线了一个HTTPS防火墙,用来监听HTTPS流量。防火墙上线之前,邮件通知给管理层,我从我老大那里听说这个事情的时候,说这个有风险,然后意外地发现,很多人原来都不知道HTTPS防

                [转帖]关于字节序(大小端)的一点想法

                https://www.zhihu.com/people/bei-ji-85/posts 今天在一个技术群里有人问起来了,当时有一些讨论(不完全都是我个人的观点),整理一下: 为什么网络字节序(多数情况下)是大端? 早年设备的缓存很小,先接收高字节能快速的判断报文信息:包长度(需要准备多大缓存)、地

                [转帖]awk提取某一行某一列的数据

                https://www.jianshu.com/p/dbcb7fe2da56 1、提取文件中第1列数据 awk '{print $1}' filename > out.txt 2、提取前2列的文件 awk `{print $1,$2}' filename > out.txt 3、打印完第一列,然后打

                [转帖]awk 中 FS的用法

                https://www.cnblogs.com/rohens-hbg/p/5510890.html 在openwrt文件 ar71xx.sh中 查询设备类型时,有这么一句, machine=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /

                [转帖]Windows Server 2022 简体中文版、英文版下载 (updated Oct 2022)

                https://sysin.org/blog/windows-server-2022/ Windows Server 2022 正式版,2022 年 10 月更新,VLSC Posted by sysin on 2022-10-27 Estimated Reading Time 8 Minutes