简体   繁体   中英

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.

As well as I have almost 30,000 records in database table and mysql is:

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. 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

EDIT : your problem may come from 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

Performance

Now let's see what happends to our performance. We have 3 different queries for solving our problems.

  • Q1. ORDER BY RAND()
  • Q2. RAND() * MAX(ID)
  • Q3. RAND() * MAX(ID) + ORDER BY ID

Q1 is expected to cost N * log2(N), Q2 and Q3 are nearly constant.

The get real values we filled the table with N rows ( one thousand to one million) and executed each query 1000 times.

Rows ||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

As you can see the plain ORDER BY RAND() is already behind the optimized query at only 100 rows in the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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