# 缓存表

__*Cached Table*__

+ 将表的内容完全缓存到 TiDB Server 的内存中
+ 表的数据量不大,几乎不更改
+ 读取很频繁
+ 缓存控制: `ALTER TABLE table_name CACHE|NOCACHE;`

```8
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 | 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)
```

------------------------------------------------------------------------------
该功能适用于频繁被访问且很少被修改的热点小表,即把整张表的数据加载到 TiDB 服务器的内存中,直接从内存中获取表数据,避免从 TiKV 获取表数据,从而提升读性能。

除了 TRACE,你也可以使用 EXPLAIN ANALYZE 观察 cache hit ratio 来获得内存访问的线索。

值得注意的是, 缓存后的表不支持 DDL 操作。比如:

```
tidb> drop table test.c1;
ERROR 8242 (HY000): 'Drop Table' is unsupported on cache tables.
```