简体   繁体   English

提高mysql中的查询性能

[英]increase query performance in mysql

I have used this query 我使用过这个查询

SELECT  COUNT(CASE WHEN C <= 1 THEN 1 END) AS nooffamiliesHavingcount1,
        COUNT(CASE WHEN C BETWEEN 2 AND 4 THEN 1 END) AS nooffamiliesHavingcountbetween2And4,
        COUNT(CASE WHEN C > 4 THEN 1 END) AS nooffamiliesHavingcountgreaterthan3
FROM    (   SELECT  COUNT(*) AS C
            FROM    user where user_id = (select user_id from location where location_id in(select location_id from country where state_name='STATE'))
            GROUP BY House_No
        ) t

Here sub query returning approximately 10000 records . 这里的子查询返回大约10000条记录。 The user table has 10,00,000 records. 用户表有10,00,000条记录。 It is taking too much time.Then the error it is saying is server gone away. 这花费了太多时间。然后它说错误是服务器消失了。 I am using mysql. 我正在使用mysql。

I searched from google.But no luck for me. 我从谷歌搜索过。但对我来说没有运气。

What changes i need to do for my tables.How i can execute this query successfully by increasing the query performance.Please suggest me.Thanks in advance.... 我需要对我的表进行哪些更改。如何通过提高查询性能来成功执行此查询。请提示我。谢谢提前....

Try this query 试试这个查询

SELECT 
  COUNT(CASE WHEN C <= 1 THEN 1 END) AS nooffamiliesHavingcount1,
  COUNT(CASE WHEN C BETWEEN 2 AND 4 THEN 1 END) AS nooffamiliesHavingcountbetween2And4,
  COUNT(CASE WHEN C > 4 THEN 1 END) AS nooffamiliesHavingcountgreaterthan3
FROM  
  (SELECT 
    COUNT(*) AS C
  FROM 
    user u,  
    location l, 
    country c 
  where 
    l.state_name='STATE' AND 
    l.some_other_column_id= 4 AND  <------- Add your condition
    c.location_id = l.location_id AND 
    u.user_id = l.user_id 
  GROUP BY 
    u.House_No) t

Use proper joins as it will be easy to understand.. 使用正确的连接,因为它很容易理解..

SELECT 
  COUNT(CASE WHEN C <= 1 THEN 1 END) AS nooffamiliesHavingcount1,
  COUNT(CASE WHEN C BETWEEN 2 AND 4 THEN 1 END) AS nooffamiliesHavingcountbetween2And4,
  COUNT(CASE WHEN C > 4 THEN 1 END) AS nooffamiliesHavingcountgreaterthan3
FROM  
  (SELECT 
    COUNT(*) AS C
  FROM 
    user u
  INNER JOIN  
    location l
  ON 
    l.state_name='STATE' AND 
    l.some_other_column_id= 4   <------- Add your condition
    u.user_id = l.user_id 
  INNER JOIN
    country c 
  ON 
    c.location_id = l.location_id 
  GROUP BY 
    u.House_No) t

EDITED EDITED

In most cases JOINs are faster than sub-queries and it is very rare for a sub-query to be faster. 在大多数情况下,JOIN比子查询更快,并且子查询的速度非常快。 I accept using subquery is more logical and easy to understand but when it comes about performance it is not as good as joins. 我接受使用子查询更合乎逻辑且易于理解,但是当它涉及性能时,它不如连接那么好。 If you are using joins your db will optimize your query on its own which is not in the case of subquery. 如果您正在使用连接,您的数据库将自行优化您的查询,这不是子查询的情况。 Try using explain for both of your query and you will get clear idea how the query executes. 尝试对您的两个查询使用explain,您将清楚地了解查询是如何执行的。

Hope this helps... 希望这可以帮助...

Can you try below: 你能尝试下面的:

SELECT COUNT(CASE WHEN COUNT( ) <= 1 THEN 1 END) AS nooffamiliesHavingcount1, COUNT(CASE WHEN COUNT( ) BETWEEN 2 AND 4 THEN 1 END) AS nooffamiliesHavingcountbetween2And4, COUNT(CASE WHEN COUNT(*) > 4 THEN 1 END) AS nooffamiliesHavingcountgreaterthan3 FROM User user Inner JOIN (select user_id from location loc Inner Join country con on loc.location_id =con.location_id where state_name='STATE' )as temp SELECT COUNT(当COUNT( )<= 1那么1结束时的情况)作为nooffamiliesHavingcount1,COUNT(在 2和4之间COUNT( )的情况下1结束)作为nooffamiliesHavingcount Between2And4,COUNT(COE(*)> 4那么结束时的情况) AS nooffamiliesHavingcountgreaterthan3 FROM用户用户内部联接(从loc loc选择user_id,在loc.location_id = con.location_id,其中state_name ='STATE')

on user.user_id =temp.user_id group by House_No 在House_No上的user.user_id = temp.user_id组中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM