posts - 167,  comments - 30,  trackbacks - 0
-- 以下两个查询效率相当,查询时间均为0.0xx秒,~ 但是Hibernate HQL不支持from后面接子查询,可以直接使用SQL方式解决,ibatis当然可以用了。
SELECT count(*from (
SELECT sum(t2.STATE) as vnSum, t1.ID FROM
vnetinfo_table t1, vm_vn_table t2 
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t where t.vnSum = 0;

SELECT count(*) FROM vnetinfo_table v ,(
SELECT sum(t2.STATE) as vnSum, t1.ID as idd FROM
vnetinfo_table t1, vm_vn_table t2 
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t
where v.ID = t.idd and t.vnSum = 0;

-- 查询效率太低了,查询时间均为0.5xx秒左右。要比以上两个SQL耗时很多倍。~ Hibernate支持在where后用子查询作为查询条件。
SELECT count(*) FROM vnetinfo_table v WHERE v.id in 
(SELECT CASE WHEN SUM(t2.STATE)=0 THEN t1.ID ELSE '' END
FROM vnetinfo_table t1, vm_vn_table t2 
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID);

-- 使用join方式替换in 查询时间均为0.09x秒左右。明显提高了效率,但是Hibernate HQL任然不支持在这种联合子查询方式。
SELECT count(*) FROM vnetinfo_table v INNER JOIN (SELECT CASE WHEN SUM(t2.STATE)=0 THEN t1.ID ELSE '' END as idd
FROM vnetinfo_table t1, vm_vn_table t2 
WHERE t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t ON v.ID = t.idd and t.idd IS not NULL;

附:Mysql中关于Exists用法的介绍请参见:http://www.nowamagic.net/librarys/veda/detail/639
posted on 2013-08-21 13:47 David1228 阅读(3744) 评论(2)  编辑  收藏

FeedBack:
# re: Mysql中使用Join和In测试
2013-08-22 08:53 | Plant Extract
这个还可以的呀  回复  更多评论
  
# re: Mysql中使用Join和In测试
2013-08-22 16:06 | essay writing service uk
Essay Writer has been offering professional writing and editing services for nearly eight years now.
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2013年8月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(4)

随笔分类

随笔档案

文章档案

新闻分类

新闻档案

相册

收藏夹

Java

Linux知识相关

Spring相关

云计算/Linux/虚拟化技术/

友情博客

多线程并发编程

开源技术

持久层技术相关

搜索

  •  

积分与排名

  • 积分 - 353973
  • 排名 - 155

最新评论

阅读排行榜

评论排行榜