当前位置:   article > 正文

clickhouse常用sql_clickhouse慢sql

clickhouse慢sql

1、慢查询监控

  1. SELECT
  2. user,
  3. formatDateTime(query_start_time, '%Y%m%d %T') AS start_time,
  4. query_duration_ms / 1000 AS query_duration_s,
  5. query,
  6. memory_usage / 1024 / 1024 / 1024 AS memory_usage_g,
  7. result_rows ,
  8. formatReadableSize(result_bytes) AS result_bytes,
  9. read_rows ,
  10. read_bytes / 1024 / 1024 /1024 AS read_bytes_g,
  11. written_rows ,
  12. written_bytes / 1024 / 1024 /1024 AS written_bytes_g
  13. FROM system.query_log
  14. WHERE type = 2
  15. and query_start_time>=today()
  16. ORDER BY query_duration_s DESC
  17. LIMIT 10

2、清空查询日志

ALTER table system.query_log delete WHERE 1=1;

3、查看数据压缩率

  1. select
  2. sum(rows) as "总行数",
  3. formatReadableSize(sum(data_uncompressed_bytes)) as "原始大小",
  4. formatReadableSize(sum(data_compressed_bytes)) as "压缩大小",
  5. round(sum(data_compressed_bytes) / sum(data_uncompressed_bytes) * 100, 0) "压缩率"
  6. from system.parts;

4、查看表大小

  1. SELECT
  2. table,
  3. formatReadableSize(sum(data_compressed_bytes)) AS compressed_size ,
  4. formatReadableSize(sum(data_uncompressed_bytes)) AS uncompressed_bytes
  5. FROM
  6. system.parts
  7. WHERE
  8. active
  9. AND (table LIKE 'data_%')
  10. GROUP BY table
  11. order by
  12. uncompressed_bytes desc ;

5、 查看当前连接数

SELECT * FROM system.metrics WHERE metric LIKE '%Connection';

6、手动合并分区

OPTIMIZE TABLE table [PARTITION partition] [FINAL]

7、添加或者更新TTL

ALTER TABLE table1 MODIFY TTL toDate(time) + toIntervalDay(10)

8、添加多个列

ALTER TABLE table1 ADD COLUMN longcol1 Int64 AFTER col111, ADD COLUMN longcol2 Int64 AFTER longcol1;

9、卸载装载分区

  1. ALTER TABLE table1 DETACH PARTITION '1629111600';
  2. ALTER TABLE table1 ATTACH PARTITION '1629111600';

10、查看表资源占用情况

  1. SELECT
  2. database,
  3. table,
  4. sum(rows) AS `总行数`,
  5. formatReadableSize(sum(data_uncompressed_bytes)) AS `原始大小`,
  6. formatReadableSize(sum(data_compressed_bytes)) AS `压缩大小`,
  7. round((sum(data_compressed_bytes) / sum(data_uncompressed_bytes)) * 100., 2) AS `压缩率/%`
  8. FROM system.parts
  9. GROUP BY
  10. database,
  11. table
  12. ORDER BY database ASC

11、个列字段占用空间统计

  1. SELECT
  2. database,
  3. table,
  4. column,
  5. any(type),
  6. sum(column_data_compressed_bytes) AS compressed,
  7. sum(column_data_uncompressed_bytes) AS uncompressed,
  8. round(uncompressed / compressed, 2) AS ratio,
  9. compressed / sum(rows) AS bpr,
  10. sum(rows)
  11. FROM system.parts_columns
  12. WHERE active AND database != 'system'
  13. GROUP BY
  14. database,
  15. table,
  16. column
  17. ORDER BY
  18. database ASC,
  19. table ASC,
  20. column ASC

12、查看后台执行的更新语句

  1. SELECT
  2. database,
  3. table,
  4. mutation_id,
  5. command,
  6. create_time,
  7. parts_to_do_names,
  8. parts_to_do,
  9. latest_fail_reason
  10. FROM
  11. system.mutations
  12. where
  13. is_done <> 1

13、查看正在执行的查询语句

  1. SELECT
  2. query_id,
  3. user,
  4. address,
  5. elapsed,
  6. query
  7. FROM
  8. system.processes
  9. ORDER BY
  10. query_id ASC

14、kill指定的查询语句

KILL QUERY WHERE query_id='query_id';

15、查看集群分布式信息

select * from system.clusters;

16、改名

rename table TABLE1 to TABLE2;

17、更新数据

ALTER TABLE db.test UPDATE name='ccc' WHERE createTime='2020/2/13';

18、系统事件、查询总次数、insert次数、失败次数

select * from system.events

19、查看建表ddl

SHOW CREATE TABLE table1 

20、查看当前查询执行列表

show processlist;

21、压缩率

  1. select
  2. database as `数据库`,
  3. table as `表名`,
  4. formatReadableSize(size) as `所占磁盘大小`,
  5. formatReadableSize(data_uncompressed_bytes) as `原始大小`,
  6. formatReadableSize(data_compressed_bytes) as `压缩大小`,
  7. compress_rate as `压缩率`,
  8. rows as `行数`,
  9. days as `存在天数`,
  10. formatReadableSize(avgDaySize) as `平均每天的大小`
  11. from
  12. (
  13. select
  14. database,
  15. table,
  16. sum(bytes) as size,
  17. sum(rows) as rows,
  18. min(min_date) as min_date,
  19. max(max_date) as max_date,
  20. sum(data_uncompressed_bytes) as data_uncompressed_bytes,
  21. sum(data_compressed_bytes) as data_compressed_bytes,
  22. (data_compressed_bytes / data_uncompressed_bytes) * 100 as compress_rate,
  23. max_date - min_date as days,
  24. size / (max_date - min_date) as avgDaySize
  25. from system.parts
  26. where active
  27. and database = 'ds'
  28. and table = 'table'
  29. group by
  30. database,
  31. table
  32. )

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/273305
推荐阅读
相关标签
  

闽ICP备14008679号