我以前介绍过最多的就是单表随机查询时的优化了,今天 看到一站长分享了一个多表随机查询优化的一些方法与方案,下面我们就一起来看看吧.
本文主要谈论如何实现mysql的随机查询,多表随机查询。在mysql中随机取出一条记录的实现方法。
我们通常的查询是没有where或者where fields>2这样的方式,这样只能取出在某种条件下的一条或多条,如果条件不变(例如2),那么结果就一直不会有变化。
那么如何实现随机查询呢?本人有两种方法。
方法一、数据表记录不大的情况下:
select * from `table`
查出来所有的记录列表,然后array_rand()随机出一个结果的数组的key。连续的key可以使用mt_rand(1, count($list)); 为什么不使用rand而是mt_rand呢?因为mt_rand币rand快4倍。
这种情况下,查出整个列表,存入到memcache的缓存或者redis的nosql中,下次直接取出结果集而不需要查表。不过当数据量一旦超过万级别,取出列表就很困难了。
方法二:使用sql语句随机
mysql函数rand(),产生一个0-1之间的小数,然后max(`id`)可以获得该表中最大的id。那么max(`id`) * rand()就可以取到表中所有的id。ok,看语句。
select * from `table` where `id` > (select rand() * (select max(`id`) from `table`) limit 0, 1
既然max(`id`) * max(`id`)可以取到表里所有值,那么本语句的where就可以取到本表的所有情况,那么这就是一个所有记录都有可能被取到的随机sql语句。
补充另一篇文章
1. 多表连接类型
1. 笛卡尔积(交叉连接) 在mysql中可以为cross join或者省略cross即join,或者使用',' 如:
select * from table1 cross join table2
select * from table1 join table2
select * from table1,table2
由于其返回的结果为被连接的两个数据表的乘积,因此当有where, on或using条件的时候一般不建议使用,因为当数据表项目太多的时候,会非常慢。一般使用left [outer] join或者right [outer] join
2. 内连接inner join 在mysql中把inner join叫做等值连接,即需要指定等值连接条件在mysql中cross和inner join被划分在一起。 join_table: table_reference [inner | cross] join table_factor [join_condition]
3. mysql中的外连接,分为左外连接和右连接,即除了返回符合连接条件的结果之外,还要返回左表(左连接)或者右表(右连接)中不符合连接条件的结果,相对应的使用null对应。
例子:
user表:
id | name
———
1 | libk
2 | zyfon
3 | daodao
user_action表:
user_id | action
—————
1 | jump
1 | kick
1 | jump
2 | run
4 | swim
sql:
select id, name, action from user as u
left join user_action a on u.id = a.user_id
result:
id | name | action
——————————–
1 | libk | jump ①
1 | libk | kick ②
1 | libk | jump ③
2 | zyfon | run ④
3 | daodao | null ⑤
分析:
注意到user_action中还有一个user_id=4, action=swim的纪录,但是没有在结果中出现,
而user表中的id=3, name=daodao的用户在user_action中没有相应的纪录,但是却出现在了结果集中
因为现在是left join,所有的工作以left为准.
结果1,2,3,4都是既在左表又在右表的纪录,5是只在左表,不在右表的纪录
工作原理:
从左表读出一条,选出所有与on匹配的右表纪录(n条)进行连接,形成n条纪录(包括重复的行,如:结果1和结果3),如果右边没有与on条件匹配的表,那连接的字段都是null.然后继续读下一条。
引申:
我们可以用右表没有on匹配则显示null的规律, 来找出所有在左表,不在右表的纪录, 注意用来判断的那列必须声明为not null的。
如:
sql:
select id, name, action from user as u
left join user_action a on u.id = a.user_id
where a.user_id is null
(注意:
1.列值为null应该用is null 而不能用=null
2.这里a.user_id 列必须声明为 not null 的.
)
上面sql的result:
id | name | action
————————–
3 | daodao | null
——————————————————————————–
一般用法:
a. left [outer] join:
除了返回符合连接条件的结果之外,还需要显示左表中不符合连接条件的数据列,相对应使用null对应
select column_name from table1 left [outer] join table2 on table1.column=table2.column
b. right [outer] join:
right与left join相似不同的仅仅是除了显示符合连接条件的结果之外,还需要显示右表中不符合连接条件的数据列,相应使用null对应
select column_name from table1 right [outer] join table2 on table1.column=table2.column
tips:
1. on a.c1 = b.c1 等同于 using(c1)
2. inner join 和 , (逗号) 在语义上是等同的
3. 当 mysql 在从一个表中检索信息时,你可以提示它选择了哪一个索引。
如果 explain 显示 mysql 使用了可能的索引列表中错误的索引,这个特性将是很有用的。
通过指定 use index (key_list),你可以告诉 mysql 使用可能的索引中最合适的一个索引在表中查找记录行。
可选的二选一句法 ignore index (key_list) 可被用于告诉 mysql 不使用特定的索引。如:
mysql> select * from table1 use index (key1,key2)
-> where key1=1 and key2=2 and key3=3;
mysql> select * from table1 ignore index (key3)
-> where key1=1 and key2=2 and key3=3;
2. 表连接的约束条件
添加显示条件where, on, using
1. where子句
mysql>
select * from table1,table2 where table1.id=table2.id;
2. on
mysql>
select * from table1 left join table2 on table1.id=table2.id;
select * from table1 left join table2 on table1.id=table2.id
left join table3 on table2.id=table3.id;
3. using子句,如果连接的两个表连接条件的两个列具有相同的名字的话可以使用using
例如:
select from left join using ()
连接多于两个表的情况举例:
mysql>
select artists.artist, cds.title, genres.genre
from cds
left join genres n cds.genreid = genres.genreid
left join artists on cds.artistid = artists.artistid;
或者 mysql>
select artists.artist, cds.title, genres.genre
from cds
left join genres on cds.genreid = genres.genreid
left join artists -> on cds.artistid = artists.artistid
where (genres.genre = 'pop');
--------------------------------------------
先过滤条件然后再根据表连接 同时在表中建立相关查询字段的索引这样在大数据多表联合查询的情况下速度相当快
select m.*,ss.sensorcode,ss.sensorstatus,ss.manufacturerid,ss.electricity,
ss.voltage,ss.minelectricity,ss.minvoltage,ss.temperature,ss.statusupdtedate,ss.updatestatus ,tp.pricingstrategyid,tps.freeduration,bat.berthtypeid
from
(select t.* , bs.parkstatus,bs.changetime ,ca.cantonname, se.sectionname
from
(select a.* ,b.berthid,b.berthcode,b.berthaddress,b.berthstatus,b.linedirection,b.cantonid,b.sectionid
from
(select ar.areaid,ar.areacode,ar.areaname from sys_area as ar where 1=1 and ar.areacode='110' ) a
left join sys_berth as b on b.areaid=a.areaid ) t
join sys_berthstatus as bs on t.berthcode=bs.berthcode
join sys_canton as ca on t.cantonid=ca.cantonid
join sys_section as se on t.sectionid=se.sectionid )m
left join sys_sensor ss on m.berthcode=ss.berthcode
left join tra_pricingberth as tp on tp.berthcode=m.berthcode
left join tra_pricingstrategy as tps on tps.pricingstrategyid=tp.pricingstrategyid
left join sys_berthandtype as bat on bat.berthcode=m.berthcode
order by berthcode asc
另外需要注意的地方 在mysql中涉及到多表查询的时候,需要根据查询的情况,想好使用哪种连接方式效率更高。
1. 交叉连接(笛卡尔积)或者内连接 [inner | cross] join
2. 左外连接left [outer] join或者右外连接right [outer] join 注意指定连接条件where, on,using.
3. mysql如何优化left join和right join
在mysql中,a left join b join_condition执行过程如下:
1)· 根据表a和a依赖的所有表设置表b。
2)· 根据left join条件中使用的所有表(除了b)设置表a。
3)· left join条件用于确定如何从表b搜索行。(换句话说,不使用where子句中的任何条件)。
4)· 可以对所有标准联接进行优化,,只是只有从它所依赖的所有表读取的表例外。如果出现循环依赖关系,mysql提示出现一个错误。
5)· 进行所有标准where优化。
6)· 如果a中有一行匹配where子句,但b中没有一行匹配on条件,则生成另一个b行,其中所有列设置为null。
