mysql创建索引及查看表大小的方法


-- 查看数据库正在执行的操作
SELECT * FROM information_schema.processlist WHERE state <> '' AND command <> 'Sleep';
-- 给具体表创建索引
CREATE INDEX dt_ip USING BTREE ON mydb.mytable (dt,ip);
-- 查看具体表的索引
SHOW INDEX FROM mydb.mytable;
-- 删除具体表的具体索引
ALTER TABLE mydb.mytable DROP INDEX dt_ip;
-- 查看mysql当前所有连接数
SELECT COUNT(*) AS connections FROM information_schema.processlist WHERE Command <> 'Sleep';
SELECT * FROM information_schema.processlist WHERE Command <> 'Sleep';
-- 查看具体语句是否命中索引
explain select name, address, age from mydb.mytable where name='serena' and  dt = '2023-12-20' ;
-- 查看索引使用情况
SELECT
    `table_schema`,
    `table_name`,
    `index_name`,
    `seq_in_index`,
    `column_name`,
    `cardinality`,
    `index_type`,
    `index_comment`
FROM
    `information_schema`.`statistics`
WHERE
    `table_schema` NOT IN ('sys', 'performance_schema', 'information_schema')
ORDER BY
    `table_schema`,
    `table_name`,
    `index_name`,
    `seq_in_index`;
-- 查看所有表的大小
SELECT
    `table_schema`,
    `table_name`,
    `table_rows`,
    `data_length`,
    `index_length`
FROM
    `information_schema`.`tables`
WHERE
    `table_schema` NOT IN ('sys', 'performance_schema', 'information_schema')
ORDER BY
    `data_length` + `index_length` DESC;

你可能感兴趣的:(mysql,mysql)