mysql8cookbook书评_MySQL cookbook第8章读书笔记

1,使用count函数生成摘要

函数函数使用很容易,对于MyISAM表来说count(*)语句很快,但是对于BDB或者InnoDB表来说,尽可能要避免使用它,因为该语句要求执行完整的扫描,速度很慢,解决办法是从information_schema数据库中提取table_rows数目

b2dcb51ba7f223c116c010c43a933f07.png

与if语句搭配使用:

d3cc6d67aa87638a120898af89a61953.png

创建视图来简化使用摘要(经常使用摘要的时候):

86ce680e6e285602cae9dc6a9d0eddde.png

使用视图:

ec9d419af7b5265465385f7f6f013785.png

使用min(),max()也是类似的:

d0714cf56a234bf5706f07186c6bb8d1.png

类似的还有sum()和avg()函数生成摘要:

9c6cbb80256734b5b62e3e1be8360741.png

359a7ba9e97a5e3c31da945bb6dc790c.png

2,使用distinct函数消除重复

adaeae819f9b4262d91e7cc5f42b6e38.png

3,查找数值相关的最大子和最小值

min(),max()这样的聚类函数并不能在where子句中使用,但是我们会有求类似于最大人口的数值,并且对应的城市名字,可以通过最大人口数值存储在用户定义的变量中,然后将数据行与该变量数值相比较来解决该问题:

f4288743b06d48c001432ecd67045ba0.png

9b637118af4ad6b46ea6587ef1d0da0f.png

还有另外一种方法从包含最小或者最大数值的数据行中选取其他数据列的方法:使用连接,将选择的数据放置到另一张表中,然后将该表与原始表接起来匹配该数值的数据行。

64174906055eb4ede3f37564aff85fbf.png

4,将摘要划分为子群

85c79a5339f6bb2c018c63129e87da59.png

为了更准确找出每个发送者从每个主机发送了多少信息,应该使用两个数据列来划分子群。

6c455f9f22de27f0290a0a7c22258902.png

这里的count是每个发送者(srcuser)从每个主机(srchost)发送的字数。

对mail表中的数据行针对srcuser进行子群划分,然后显示每个子群中发送的最大的消息的大小和最小的消息的大小

25d627f1085e1d4aaa46f97eca026d1b.png

经常出现的一种问题:当使用group by来划分子群时,唯一能选择的子句是划分子群的子句和从子群中计算得到的摘要数值。

d212533eda84a34e7725fc020527679c.png

红色标记为错误,因为子群划分为name,而显示中存在trav_date不是划分子群的子句和从子群中计算得到的摘要数值,显示与最小最大子群数值相关的数据行通常使用引入连接的技术:

290e764b9b1c4837ceb76b704d86ce92.png

1c5d5c71428dfc66ffe67d68c4a1a3ff.png

5,使用确定的特性选择组群

75015f96979aa1f77ec4200b644ec75c.png

where 和having的区别在于having操作在已经选定和划分好子群的的数据行集上,能够对聚类子群进行额外的条件约束。

acf07a1ae411a2163d7605ab65813c2b.png

当然也可以使用别名:

03ec9d15a36e9698cd113847a2388163.png

使用having和count确定某一个子群内某项纪录唯一:

938bd956426dda775c3e2b48432057c4.png

也可以使用与联合数值中。例如:为了查找仅仅发送了一个信息的信息发送/接收对,可以在mail表中查找仅仅发生一次的联合:

0dfa88fbf336fe5aa5556dfb0bd76b58.png

还可以使用表达式结果分组:

bc06754ce1a8a20f0f0799351f74e87b.png

b5dd2338c35dddaad9f296ab49989de0.png

6,分类无类别数据

比如在无重复数据中,例如:

649c2b1115209e8811603a4e31d67a21.png

不存在重复的数,故不好使用group by来划分子群。

0d5a9035ea13cf402b8df6a653e11e0e.png

将人口划分为5个百万量级:?????

7,控制摘要显示顺序

de35852ab9b762d1c88197bf2e9872ab.png

以名字划分子群,以行驶的天数来进行排序:

af5e48ee4df1312cc34327d88cd9384f.png

8,查找最小或最大的摘要数值

min(),max()不能作为其他聚类函数的参数。例如:你很容易查找到每个司机的总行驶英里数:

c8a2ecaade0f9e3369c44880d3e53d6a.png

a25870d80c13b6ea2f988f458957089e.png不能够正常工作,

但是可以先对数据行排序,然后使用limit选择第一个数据行:

334856cb25424f7414393c0052820146.png

9,生成包括摘要和列表的报告

使用pthon生成摘要和列表的报告:

import Cookbook

import sys

import MySQLdb

name_map={}

conn=Cookbook.connect()

cursor=conn.cursor()

cursor.execute("""

select name, count(name),sum(miles)

from driver_log group by name

""")

for(name,days, miles) in cursor.fetchall():

name_map[name]=(days,miles)

# 选择每个司机的行程数并打印报告,显示行程列表中每个司机的摘要汇总项

cursor.execute("""

select name, trav_date, miles

from driver_log group by name, trav_date

""")

cur_name=""

for (name, trav_date, miles) in cursor.fetchall():

if cur_name !=name:

print "Name: %s; days on road: %d; miles drien:%d "\

%(name, name_map[name][0],name_map[name][1])

cur_name=name

print "date:%s, trip length:%d "%(trav_date,miles)

cursor.close()

75a311239796f21c55e87f0d3bd57224.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration Design and administer enterprise-grade MySQL 8 solutions Key Features Store, retrieve, and manipulate your data using the latest MySQL 8 features Practical recipes on effective administration in MySQL, with a focus on security, performance tuning, troubleshooting, and more Contains tips, tricks, and best practices for designing, developing, and administering your MySQL 8 database solution without any hassle Book Description MySQL is one of the most popular and widely used relational databases in the World today. The recently released MySQL 8 version promises to be better and more efficient than ever before. This book contains everything you need to know to be the go-to person in your organization when it comes to MySQL. Starting with a quick installation and configuration of your MySQL instance, the book quickly jumps into the querying aspects of MySQL. It shows you the newest improvements in MySQL 8 and gives you hands-on experience in managing high-transaction and real-time datasets. If you've already worked with MySQL before and are looking to migrate your application to MySQL 8, this book will also show you how to do that. The book also contains recipes on efficient MySQL administration, with tips on effective user management, data recovery, security, database monitoring, performance tuning, troubleshooting, and more. With quick solutions to common and not-so-common problems you might encounter while working with MySQL 8, the book contains practical tips and tricks to give you the edge over others in designing, developing, and administering your database effectively. What you will learn Install and configure your MySQL 8 instance without any hassle Get to grips with new features of MySQL 8 like CTE, Window functions and many more Perform backup tasks, recover data and set up various replication topologies for your database Maximize performance by using new features o

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值