LIMIT

LIMIT 用于限制查询返回的行数。当处理大数据集时该子句特别有用,因为它通过减少需要处理的数据量来提高查询性能。

Syntax

LIMIT 的基本语法如下:

  1. SELECT column1, column2, ...
  2. FROM table_name
  3. LIMIT number_of_rows;

number_of_rows 参数用于指定要返回的最大行数。如果该参数的值为负数,则不返回任何行。

示例

假如我们有一个名为 system_metrics 的表:

  1. +-------+-------+----------+-------------+-----------+---------------------+
  2. | host | idc | cpu_util | memory_util | disk_util | ts |
  3. +-------+-------+----------+-------------+-----------+---------------------+
  4. | host1 | idc_a | 11.8 | 10.3 | 10.3 | 2022-11-03 03:39:57 |
  5. | host1 | idc_b | 50 | 66.7 | 40.6 | 2022-11-03 03:39:57 |
  6. | host1 | idc_c | 50.1 | 66.8 | 40.8 | 2022-11-03 03:39:57 |
  7. | host1 | idc_e | NULL | 66.7 | 40.6 | 2022-11-03 03:39:57 |
  8. | host2 | idc_a | 80.1 | 70.3 | 90 | 2022-11-03 03:39:57 |
  9. +-------+-------+----------+-------------+-----------+---------------------+

使用 LIMIT 获取 memory_util 列中的前 3 个值:

  1. SELECT host, idc, memory_util
  2. FROM system_metrics
  3. ORDER BY memory_util DESC
  4. LIMIT 3;

结果为:

  1. +-------+-------+-------------+
  2. | host | idc | memory_util |
  3. +-------+-------+-------------+
  4. | host2 | idc_a | 70.3 |
  5. | host1 | idc_c | 66.8 |
  6. | host1 | idc_b | 66.7 |
  7. +-------+-------+-------------+

LIMIT n, m 允许在跳过前 n 行后从结果中选择 m 行,等价于LIMIT m OFFSET n 语法。

  1. SELECT host, idc, memory_util
  2. FROM system_metrics
  3. ORDER BY memory_util DESC
  4. LIMIT 2 OFFSET 1;

  1. SELECT host, idc, memory_util
  2. FROM system_metrics
  3. ORDER BY memory_util DESC
  4. LIMIT 1, 2;

结果如下:

  1. +-------+-------+-------------+
  2. | host | idc | memory_util |
  3. +-------+-------+-------------+
  4. | host1 | idc_c | 66.8 |
  5. | host1 | idc_b | 66.7 |
  6. +-------+-------+-------------+