MYSQL子查询和嵌套查询优化实例解析

作者:dodomail 时间:2024-01-23 13:24:34 

查询游戏历史成绩最高分前100

Sql代码


SELECT ps.* FROM cdb_playsgame ps WHERE ps.credits=(select MAX(credits)  
FROM cdb_playsgame ps1  
where ps.uid=ps1.uid AND ps.gametag=ps1.gametag) AND ps.gametag='yeti3'  
GROUP BY ps.uid order by ps.credits desc LIMIT 100;

Sql代码


SELECT ps.*  
FROM cdb_playsgame ps,(select ps1.uid, ps1.gametag, MAX(credits) as credits
FROM cdb_playsgame ps1 group by uid,gametag) t
WHERE ps.credits=t.credits AND ps.uid=t.uid AND ps.gametag=t.gametag AND ps.gametag='yeti3'  
GROUP BY ps.uid order by ps.credits desc LIMIT 100;

执行时间仅为0.22秒,比原来的25秒提高了10000倍

查询当天游戏最好成绩

Sql代码


SELECT ps. * , mf. * , m.username
FROM cdb_playsgame ps
LEFT JOIN cdb_memberfields mf ON mf.uid = ps.uid
LEFT JOIN cdb_members m ON m.uid = ps.uid
WHERE ps.gametag = 'chuansj'
AND FROM_UNIXTIME( ps.dateline, '%Y%m%d' ) = '20081008'
AND ps.credits = (
SELECT MAX( ps1.credits )
FROM cdb_playsgame ps1
WHERE ps.uid = ps1.uid
AND ps1.gametag = 'chuansj'
AND FROM_UNIXTIME( ps1.dateline, '%Y%m%d' ) = '20081008' )
GROUP BY ps.uid
ORDER BY credits DESC
LIMIT 0 , 50

像查询里:


AND ps.credits=(SELECT MAX(ps1.credits)  
FROM {$tablepre}playsgame ps1 where ps.uid=ps1.uid AND ps1.gametag = '$game'  
AND FROM_UNIXTIME(ps1.dateline, '%Y%m%d') = '$todaytime' )

特别消耗时间

另外,像:


FROM_UNIXTIME(ps1.dateline, '%Y%m%d') = '$todaytime'

这样的语句会导致索引无效,因为对每个dataline的值都需要用函数计算一遍,需要调整为:

Sql代码


AND ps1.dateline >= UNIX_TIMESTAMP('$todaytime')

//更改后
Sql代码


SELECT ps. * , mf. * , m.username
FROM cdb_playsgame ps, cdb_memberfields mf, cdb_members m, (

SELECT ps1.uid, MAX( ps1.credits ) AS credits
FROM cdb_playsgame ps1
WHERE ps1.gametag = 'chuansj'
AND ps1.dateline >= UNIX_TIMESTAMP( '20081008' )
GROUP BY ps1.uid
) AS t
WHERE mf.uid = ps.uid
AND m.uid = ps.uid
AND ps.gametag = 'chuansj'
AND ps.credits = t.credits
AND ps.uid = t.uid
GROUP BY ps.uid
ORDER BY credits DESC
LIMIT 0 , 50

对于每个球员,找出球员号码,名字以及他所引起的罚款的号码,但只是针对那些至少有两次罚款的球员。

更紧凑的查询,在FROM子句中放置一个子查询。

Sql代码


SELECT PLAYERNO,NAME,NUMBER
FROM (SELECT PLAYERNO,NAME,
      (SELECT COUNT(*)
      FROM PENALTIES
      WHERE PENALTIES.PLAYERNO =
         PLAYERS.PLAYERNO)
      AS NUMBER
   FROM PLYERS) AS PN
WHERE NUMBER>=2

FROM子句中的子查询决定了每个球员的号码,名字和罚款的编号。接下来,这个号码变成了中间结果中的一列。然后指定了一个条件(NUMBER>=2);最后,获取SELECT子句中的列。

总结

感谢朋友们对脚本之家网站的支持!

来源:http://dodomail.iteye.com/blog/250199

标签:mysql,子查询,嵌套
0
投稿

猜你喜欢

  • python爬虫lxml库解析xpath网页过程示例

    2021-10-06 20:01:54
  • 浅析webpack-bundle-analyzer在vue-cli3中的使用

    2024-05-28 16:09:50
  • Python3使用PySynth制作音乐的方法

    2021-03-18 19:41:01
  • python小白切忌乱用表达式

    2021-08-11 19:57:11
  • jquery判断元素是否隐藏的多种方法

    2024-04-19 10:23:51
  • Vue $emit()不能触发父组件方法的原因及解决

    2024-05-28 16:10:26
  • PHP后台备份MySQL数据库的源码实例

    2023-09-04 14:55:11
  • OpenCV中图像通道操作的深入讲解

    2022-02-13 02:39:22
  • Python Websocket服务端通信的使用示例

    2021-09-16 15:03:19
  • asp会员系统如何实现“忘记密码”

    2007-09-19 12:17:00
  • Python分支语句与循环语句应用实例分析

    2022-12-02 03:35:31
  • mysql数据库之索引详细介绍

    2024-01-19 09:06:08
  • VUE中如何动态绑定类名和样式

    2024-04-26 17:41:35
  • 分析python请求数据

    2023-09-26 08:46:08
  • thinkphp实现多语言功能(语言包)

    2024-05-22 10:05:21
  • python3安装OCR识别库tesserocr过程图解

    2021-12-08 04:50:08
  • TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法

    2021-03-20 19:55:17
  • JavaScript之Getters和Setters 平台支持等详细介绍

    2024-04-19 09:45:18
  • JavaScript对象的创建模式与继承模式示例讲解

    2024-04-23 09:27:40
  • 总结python爬虫抓站的实用技巧

    2022-07-07 05:04:09
  • asp之家 网络编程 m.aspxhome.com