百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

DBA技术分享(九)- MySQL数据库中查找最常用的数据类型

wptr33 2024-12-29 06:24 25 浏览

一、概述

今天分享几个关于MySQL数据类型的查询,具体如下:

  • 在 MySQL 数据库中查找最常用的数据类型
  • 查找 MySQL 数据库中的所有数字列
  • 查找 MySQL 数据库中的所有字符串(字符)列
  • 查找 MySQL 数据库中的所有日期和时间列
  • 查找 MySQL 数据库中的所有枚举列
  • 查找 MySQL 数据库中的所有空间数据列
  • 查找 MySQL 数据库中的所有 JSON 数据列
  • 在 MySQL 数据库中查找大对象 (LOB) 数据类型列
  • 在 MySQL 数据库中查找具有大对象 (LOB) 数据类型列的表

二、相关SQL

2.1 在 MySQL 数据库中查找最常用的数据类型

select data_type,
       count(*) as columns,
       cast(100*count(*)/sum_all.columns as decimal(36,2)) 
            as percent_columns,
       count(distinct concat(col.table_schema, '.', col.table_name))
            as tables,
       cast(100*count(distinct concat(col.table_schema,'.',col.table_name)) 
            / sum_all.tables as decimal(36,2)) as percent_tables
from information_schema.columns col
join (select count(distinct concat(c.table_schema, '.', c.table_name))
                    as tables,
             count(*) as columns
      from information_schema.columns c
      join information_schema.tables t
           on c.table_schema = t.table_schema
           and c.table_name = t.table_name
      where t.table_schema not in ('information_schema', 'mysql',
                                   'performance_schema', 'sys')
            and t.table_type = 'BASE TABLE'
      ) sum_all on true
join information_schema.tables tab
     on col.table_schema = tab.table_schema
     and col.table_name = tab.table_name
where tab.table_schema not in ('information_schema', 'mysql',
                               'performance_schema', 'sys')
      and tab.table_type = 'BASE TABLE'
group by data_type,
         sum_all.columns,
         sum_all.tables
order by columns desc;

说明:

  • data_type - 没有长度或精度的内置或用户数据类型,例如 int、varchar 或 datetime
  • columns - 具有此数据类型的数据库(模式)中的列数
  • percent_columns - 具有此数据类型的列的百分比。行总数为 100%
  • tables- 数据库(模式)中具有此数据类型的表数
  • percent_tables - 具有此数据类型的列的表的百分比。


2.2 查找 MySQL 数据库中的所有数字列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as col_id,
       col.column_name,
       col.data_type,
       col.numeric_precision,
       col.numeric_scale
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
                                   and tab.table_name = col.table_name
                                   and tab.table_type = 'BASE TABLE'
where col.data_type in ('tinyint', 'smallint', 'mediumint', 
                        'int', 'bigint', 'decimal', 'bit',
                        'float', 'double')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     --and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name,
         col.ordinal_position;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 数据类型
  • numeric_precision - 列的精度
  • numeric_scale - 列的比例



2.3 查找 MySQL 数据库中的所有字符串(字符)列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as column_id,
       col.column_name,
       col.data_type,
       col.character_maximum_length as maximum_length,
       col.character_set_name
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
                                   and tab.table_name = col.table_name
                                   and tab.table_type = 'BASE TABLE'
where col.data_type in ('char', 'varchar', 'binary', 'varbinary', 
                        'blob', 'tinyblob', 'mediumblob', 'longblob',
                        'text', 'tinytext', 'mediumtext', 'longtext'
                        'enum', 'set')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     --and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name,
         col.ordinal_position;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 数据类型
  • maximum_length - 字符的最大长度
  • character_set_name - 字符集名称


2.4 查找 MySQL 数据库中的所有日期和时间列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as column_id,
       col.column_name,
       col.data_type,
       col.datetime_precision
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
                                   and tab.table_name = col.table_name
                                   and tab.table_type = 'BASE TABLE'
where col.data_type in ('date', 'time', 'datetime', 'year', 'timestamp')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     --and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name,
         col.ordinal_position;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 数据类型
  • datetime_precision - 小数秒精度

2.5 查找 MySQL 数据库中的所有枚举列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as column_id,
       col.column_name,
       col.data_type,
       trim(leading 'enum' from col.column_type) as enum_values
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
                                   and tab.table_name = col.table_name
                                   and tab.table_type = 'BASE TABLE'
where col.data_type in ('enum')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     --and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name,
         col.ordinal_position;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 数据类型
  • enum_values - 声明可能的枚举值

2.6 查找 MySQL 数据库中的所有空间数据列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as column_id,
       col.column_name,
       col.data_type,
       col.is_nullable
from information_schema.columns col
join information_schema.tables tab
     on col.table_schema = tab.table_schema
     and col.table_name = tab.table_name
     and table_type = 'BASE TABLE'
where col.data_type in ('geometry', 'point', 'linestring', 'polygon',
                        'multipoint', 'multilinestring', 'multipolygon',
                        'geometrycollection')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     -- and table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 空间数据的类型:

(1)GEOMETRY

(2)POINT

(3)LINESTRING

(4)POLYGON

(5)MULTIPOINT

(6)MULTILINESTRING

(7)MULTIPOLYGON

(8)GEOMETRYCOLLECTION

  • is_nullable - 指示列是否可以包含空值

2.7 查找 MySQL 数据库中的所有 JSON 数据列

select col.table_schema as database_name,
       col.table_name,
       col.ordinal_position as column_id,
       col.column_name,
       col.data_type
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
                                   and tab.table_name = col.table_name
                                   and tab.table_type = 'BASE TABLE'
where col.data_type in ('json')
      and col.table_schema not in ('information_schema', 'sys',
                                   'performance_schema', 'mysql')
     --and col.table_schema = 'database_name' -- put your database name here
order by col.table_schema,
         col.table_name,
         col.ordinal_position;

说明:

  • database_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_id - 表中的列位置
  • column_name - 列的名称
  • data_type - 数据类型

2.8 在 MySQL 数据库中查找大对象 (LOB) 数据类型列

select tab.table_schema as database_name,
    tab.table_name,
    col.column_name,
    col.data_type
from information_schema.tables as tab
inner join information_schema.columns as col
    on col.table_schema = tab.table_schema
    and col.table_name = tab.table_name
where tab.table_schema = 'your database name'
    and tab.table_type = 'BASE TABLE'
    and col.data_type in ('blob', 'mediumblob', 'longblob',
                         'text','mediumtext','longtext')
order by tab.table_name,
    col.column_name;

说明:

  • schema_name - 数据库的名称(模式)
  • table_name - 表的名称
  • column_name - 列的名称
  • data_type - 数据类型

2.9 在 MySQL 数据库中查找具有大对象 (LOB) 数据类型列的表

select tab.table_name,
    count(*) as columns
from information_schema.tables as tab
    inner join information_schema.columns as col
        on col.table_schema = tab.table_schema
        and col.table_name = tab.table_name
        and col.data_type in ('blob', 'mediumblob', 'longblob', 
                               'text', 'mediumtext', 'longtext')
where tab.table_schema = 'your database name'
      and tab.table_type = 'BASE TABLE'
group by tab.table_name
order by tab.table_name;

说明:

  • table_name - 表的名称
  • columns - 表中的 LOB 列数

小结

后面会分享更多Linux和DBA方面内容,感兴趣的朋友可以关注下!

相关推荐

redis的八种使用场景

前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...

基于Redis的3种分布式ID生成策略

在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...

基于OpenWrt系统路由器的模式切换与网页设计

摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...

这篇文章教你看明白 nginx-ingress 控制器

主机nginx一般nginx做主机反向代理(网关)有以下配置...

如何用redis实现注册中心

一句话总结使用Redis实现注册中心:服务注册...

爱可可老师24小时热门分享(2020.5.10)

No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...

Apportable:拯救程序员,IOS一秒变安卓

摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...

JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透

以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...

3月26日更新 快速施法自动施法可独立设置

2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...

Redis 是如何提供服务的

在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...

lua _G、_VERSION使用

到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...

China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting

BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...

移动工作交流工具Lua推出Insights数据分析产品

Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...

Redis 7新武器:用Redis Stack实现向量搜索的极限压测

当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...

Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求

重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...