数据库查询某月每天的数据量

数据库查询某月每天的数据量,第1张

set @x:=0;

select pdayspday, sum(tmoney)

from 

  (select pday from  

     (  select adddate(str_to_date('2013-09-01','%Y-%m-%d'),f1) as pday

        from (select  @x:=@x+1 as f1 from customer limit 1,31) t1

     ) t2 where pday<=last_day(str_to_date('2013-09-01','%Y-%m-%d'))

  ) pdays 

  left join 销售表 t on pdayspday=tdate

where tname='fff'

group by pdayspday

数据库中有几十上百张表,那么哪些表的数据量比较大呢,总不能一个表一个表的去查询吧,在mysql中也有类似于oracle的数据字典表,只不过mysql没有oracle记录的那么多和详细,但也足够我们查询这些信息了。

在mysql的information_schema下有存储数据库基本信息的数据字典表,可以通过查询tables表来获得所需要的表相关信息。

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

|mysql |

|report |

| report_result |

|test |

+--------------------+

5 rows in set (002 sec)

mysql> use information_schema;

Database changed

mysql> show tables;

+---------------------------------------+

|Tables_in_information_schema |

+---------------------------------------+

|CHARACTER_SETS |

|COLLATIONS |

| COLLATION_CHARACTER_SET_APPLICABILITY |

|COLUMNS |

|COLUMN_PRIVILEGES |

|KEY_COLUMN_USAGE |

|PROFILING |

|ROUTINES |

|SCHEMATA |

|SCHEMA_PRIVILEGES |

|STATISTICS |

|TABLES |

|TABLE_CONSTRAINTS |

|TABLE_PRIVILEGES |

|TRIGGERS |

|USER_PRIVILEGES |

|VIEWS |

+---------------------------------------+

17 rows in set (000 sec)

那么我们查看一下talbes表结构信息,看看存储的具体信息

mysql> desc tables;

+-----------------+--------------+------+-----+---------+-------+

| Field |Type | Null | Key | Default |Extra |

+-----------------+--------------+------+-----+---------+-------+

| TABLE_CATALOG | varchar(512) | YES | | NULL | |

| TABLE_SCHEMA | varchar(64) | NO | | | |

| TABLE_NAME | varchar(64) | NO | | | |

| TABLE_TYPE | varchar(64) | NO | | | |

| ENGINE |varchar(64) | YES | |NULL | |

| VERSION |bigint(21) | YES | |NULL | |

| ROW_FORMAT | varchar(10) | YES | | NULL | |

| TABLE_ROWS | bigint(21) | YES | | NULL | |

| AVG_ROW_LENGTH | bigint(21) | YES | | NULL | |

| DATA_LENGTH | bigint(21) | YES | | NULL | |

| MAX_DATA_LENGTH | bigint(21) | YES | | NULL | |

| INDEX_LENGTH | bigint(21) | YES | | NULL | |

| DATA_FREE | bigint(21) |YES | | NULL | |

| AUTO_INCREMENT | bigint(21) | YES | | NULL | |

| CREATE_TIME | datetime |YES | | NULL | |

| UPDATE_TIME | datetime |YES | | NULL | |

| CHECK_TIME | datetime |YES | | NULL | |

| TABLE_COLLATION | varchar(64) | YES | |NULL | |

| CHECKSUM | bigint(21) |YES | | NULL | |

| CREATE_OPTIONS | varchar(255) | YES | |NULL | |

| TABLE_COMMENT | varchar(80) | NO | | | |

+-----------------+--------------+------+-----+---------+-------+

21 rows in set (000 sec)

主要存储了表的信息如表使用的引擎,表的类型等信息。我们可以通过查询table_rows属性获得哪些表数据量比较大。

mysql> select table_name,table_rows from tables order by table_rows desc limi 10;

+---------------+------------+

| table_name |table_rows |

+---------------+------------+

| task6 | 1558845 |

| task | 1554399 |

| task5 | 1539009 |

| task3 | 1532169 |

| task1 | 1531143 |

| task2 | 1531143 |

| task4 | 1521225 |

| task7 | 980865 |

我们继续深入思考,这些存储的数据是否准确,是否真实的反应了表中数据量大小?

mysql> show create table tables \G;

1 row

Table: TABLES

Create Table: CREATE TEMPORARY TABLE`TABLES` (

`TABLE_CATALOG` varchar(512) default NULL,

`TABLE_SCHEMA` varchar(64) NOT NULL default '',

`TABLE_NAME` varchar(64) NOT NULL default '',

`TABLE_TYPE` varchar(64) NOT NULL default '',

&nb

直接在查询分析器运行即可:

declare @id int

declare @type character(2)

declare @pages

int

declare @dbname sysname

declare @dbsize dec(15,0)

declare @bytesperpage dec(15,0)

declare @pagesperMB dec(15,0)

create table #spt_space

(

objid int null,

rows int null,

reserved dec(15) null,

data dec(15) null,

indexp dec(15) null,

unused dec(15) null

)

set nocount on

-- Create a cursor to loop through the user tables

declare c_tables cursor for

select id

from sysobjects

where xtype = 'U'

open c_tables

fetch next from c_tables

into @id

while @@fetch_status = 0

begin

/ Code from sp_spaceused /

insert into #spt_space (objid, reserved)

select objid = @id, sum(reserved)

from sysindexes

where indid in (0, 1, 255)

and id = @id

select @pages = sum(dpages)

from sysindexes

where indid < 2

and id = @id

select @pages = @pages + isnull(sum(used), 0)

from sysindexes

where indid = 255

and id = @id

update #spt_space

set data = @pages

where objid = @id

/ index: sum(used) where indid in (0, 1, 255) - data /

update #spt_space

set indexp = (select sum(used)

from sysindexes

where indid in (0, 1, 255)

and id = @id)

- data

where objid = @id

/ unused: sum(reserved) - sum(used) where indid in (0, 1, 255) /

update #spt_space

set unused = reserved

- (select sum(used)

from sysindexes

where indid in (0, 1, 255)

and id = @id)

where objid = @id

update #spt_space

set rows = irows

from sysindexes i

where iindid < 2

and iid = @id

and objid = @id

fetch next from c_tables

into @id

end

select TableName = (select left(name,60) from sysobjects where id = objid),

Rows = convert(char(11), rows),

ReservedKB = ltrim(str(reserved dlow / 1024,15,0) + ' ' + 'KB'),

DataKB = ltrim(str(data dlow / 1024,15,0) + ' ' + 'KB'),

IndexSizeKB = ltrim(str(indexp dlow / 1024,15,0) + ' ' + 'KB'),

UnusedKB = ltrim(str(unused dlow / 1024,15,0) + ' ' + 'KB')

from #spt_space, masterdbospt_values d

where dnumber = 1

and dtype = 'E'

order by reserved desc

drop table #spt_space

close c_tables

deallocate c_tables

以上就是关于数据库查询某月每天的数据量全部的内容,包括:数据库查询某月每天的数据量、如何查询mysql数据库中哪些表的数据量最大、如何查看SQL2000数据库中所有表的数据量大小等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10204369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存