简体   繁体   English

MySQL超时问题

[英]MySQL time out issue

I am facing serious issue in my workout related to PHP and MySql on Linux server while when am running same code with same database in localhost, it's working fine. 我在与Linux服务器上的PHP和MySql有关的锻炼中遇到严重问题,而在本地主机中使用相同数据库运行相同代码时,它运行正常。

As well as I have almost 30,000 records in database table and mysql is: 以及我在数据库表中有近30,000条记录,而mysql是:

SELECT * FROM tbl_movies where id not in (select movie_id from tbl_usermovieque where user_id='3' union 
                                      select movie_id from tbl_user_movie_response where user_id='3' union 
                                      select movie_id from tbl_user_movie_fav where user_id='3') and id < 220 order by rand() limit 0,20

its taking 0.0010 sec in my localhost and INFINITE on our linux server. 它在我的本地主机和Linux服务器上的INFINITE中花费0.0010秒。 i unable to find the reason. 我找不到原因。

Thanks Kamal 谢谢卡马尔

Can you confirm this return the same result ? 您可以确认返回相同的结果吗? It should be faster this way. 这样应该更快。 Union are usefull sometime but not really optimized. 联合有时有用,但并未真正优化。

SELECT * FROM tbl_movies where id not in (
    select distinct movie_id
    from tbl_movies m
    inner join tbl_usermovieque um ON um.movie_id = m.movie_id = m.movie_id
    inner join tbl_user_movie_response umr ON umr.movie_id = m.movie_id = m.movie_id
    inner join tbl_user_movie_fav umf ON umf.movie_id = m.movie_id = m.movie_id
    where um.user_id = 3 or umr.user_id = 3 or umf.user_id = 3
) and id < 220 order by rand() limit 0,20;

PS : I assume you have Index un oser_id and id_movie PS:我假设您有索引un oser_id和id_movie

EDIT : your problem may come from rand() 编辑:您的问题可能来自rand()

MySQL order by optimization Look for RAND() in the page : in comment there are some performance test => rand() alone seams to be a bad solution MySQL优化排序在页面中查找RAND():在注释中有一些性能测试=> rand()单独接缝是一个不好的解决方案

Performance 性能

Now let's see what happends to our performance. 现在,让我们看看我们的表现如何。 We have 3 different queries for solving our problems. 我们有3个不同的查询来解决我们的问题。

  • Q1. Q1。 ORDER BY RAND() 按RAND()订购
  • Q2. Q2。 RAND() * MAX(ID) RAND()* MAX(ID)
  • Q3. Q3。 RAND() * MAX(ID) + ORDER BY ID RAND()* MAX(ID)+按ID排序

Q1 is expected to cost N * log2(N), Q2 and Q3 are nearly constant. 预计Q1的成本为N * log2(N),Q2和Q3几乎恒定。

The get real values we filled the table with N rows ( one thousand to one million) and executed each query 1000 times. 为了获得实数值,我们用N行(一千到一百万)填充了表,并且每个查询执行了1000次。

Rows ||100 ||1.000 ||10.000 ||100.000 ||1.000.000 行|| 100 || 1.000 || 10.000 || 100.000 || 1.000.000

Q1||0:00.718s||0:02.092s||0:18.684s||2:59.081s||58:20.000s Q2||0:00.519s||0:00.607s||0:00.614s||0:00.628s||0:00.637s Q3||0:00.570s||0:00.607s||0:00.614s|0:00.628s ||0:00.637s Q1 || 0:00.718s || 0:02.092s ||| 0:18.684s || 2:59.081s ||| 58:20.000s Q2 || 0:00.519s || 0:00.607s || 0:00.614s || 0:00.628s || 0:00.637s Q3 || 0:00.570s || 0:00.607s || 0:00.614s | 0:00.628s || 0:00.637s

As you can see the plain ORDER BY RAND() is already behind the optimized query at only 100 rows in the table. 如您所见,普通ORDER BY RAND()已经在表中仅100行的优化查询后面。

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

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