Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL] "Ciężkie" zapytanie - optymalizacja?
@Wu
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 2.08.2008

Ostrzeżenie: (0%)
-----


Witam,



W skrypcie php mam funkcję, która zawiera "ciężkie" zapytanie do bazy, po wyłączeniu ów "dodatku" strona ładuje się bardzo szybko.. Po włączeniu logowania slow queries na serwerze oczywiście owe zapytanie jest tam logowane... (IMG:http://forum.php.pl/style_emoticons/default/worriedsmiley.gif)



Funkcja wraz z zapytaniami:



  1. <?php
  2. function get_beers() 
  3. { 
  4. global $forum_id, $topic_id, $post_id, $userdata, $board_config, $db; 
  5.  
  6. if ( $this->config['topic_poster_only'] && $topic_id ) 
  7. { 
  8. $sql = " select topic_first_post_id from " . TOPICS_TABLE . " where topic_id = $topic_id"; 
  9. if ( !($results = $db->sql_query($sql)) ) 
  10. { 
  11. message_die(GENERAL_ERROR, 'Couldn't obtain topic first post id.', '', __LINE__, __FILE__, $sql); 
  12. } 
  13. $_data = $db->sql_fetchrow($results); 
  14. $this->topic_first_post_id = intval($_data['topic_first_post_id']); 
  15. } 
  16.  
  17. $user_id = $userdata['user_id']; 
  18. if ( $topic_id > 0 ) 
  19. { 
  20. $sql = " select p1.post_id as post_id_sec, b.*, u1.username as beer_poster, u2.user
    _id, u2.user_beers_src, u2.user_beers_dst, u1.user_level, u1.user_jr, 
  21. p2.poster_id as poster_id_sec, t.topic_id as topic_id_sec, f.forum_id as forum_i
    d_sec 
  22. from " . POSTS_TABLE . " p1 
  23. left join " . BEER_TABLE . " b ON b.post_id = p1.post_id 
  24. left join " . POSTS_TABLE . " p2 ON p2.post_id = b.post_id 
  25. left join " . TOPICS_TABLE . " t ON t.topic_id = b.topic_id 
  26. left join " . FORUMS_TABLE . " f ON f.forum_id = b.forum_id 
  27. left join " . USERS_TABLE . " u1 ON u1.user_id = b.beer_src 
  28. left join " . USERS_TABLE . " u2 ON u2.user_id = p1.poster_id 
  29. where p1.topic_id = $topic_id " . (($user_id > 1) ? " or b.beer_src = $user_id or b.beer_dst = $user_id" : ''); 
  30. } 
  31. else if ( $topic_id < 1 && $post_id > 0 ) 
  32. { 
  33. $sql = " select p1.post_id as post_id_sec, b.*, u1.username as beer_poster, u2.user
    _id, u2.user_beers_src, u2.user_beers_dst, u1.user_level, u1.user_jr, 
  34. p2.poster_id as poster_id_sec, t.topic_id as topic_id_sec, f.forum_id as forum_i
    d_sec 
  35. from " . POSTS_TABLE . " p0 
  36. left join " . POSTS_TABLE . " p1 ON p1.topic_id = p0.topic_id 
  37. left join " . BEER_TABLE . " b ON (b.post_id = p1.post_id ) 
  38. left join " . POSTS_TABLE . " p2 ON p2.post_id = b.post_id 
  39. left join " . TOPICS_TABLE . " t ON t.topic_id = b.topic_id 
  40. left join " . FORUMS_TABLE . " f ON f.forum_id = b.forum_id 
  41. left join " . USERS_TABLE . " u1 ON u1.user_id = b.beer_src 
  42. left join " . USERS_TABLE . " u2 ON u2.user_id = p1.poster_id 
  43. where p0.post_id = $post_id "; 
  44. } 
  45. else 
  46. { 
  47. return false; 
  48. } 
  49. if ( !($results = $db->sql_query($sql)) ) 
  50. { 
  51. message_die(GENERAL_ERROR, 'Couldn't obtain topic beers', '', __LINE__, __FILE__, $sql); 
  52. } 
  53.  
  54. $this->topic_replies = $trash_beers = array(); 
  55. while ( $row = $db->sql_fetchrow($results) ) 
  56. { 
  57. if ( $row['beer_id'] ) 
  58. { 
  59. if ( $row['beer_dst'] == $row['beer_src'] || $row['poster_id_sec'] == $row['beer_src'] || !$row['poster_id_sec'] || !$row['forum_id_sec'] || !$row['topic_id_sec'] || !$row['beer_poster'] ) 
  60. { 
  61. $trash_beers[] = $row['beer_id']; 
  62. } 
  63. else 
  64. { 
  65. if ( $row['beer_src'] == $user_id && $user_id > 1 ) $this->user_src++; 
  66. if ( $row['beer_dst'] == $user_id && $user_id > 1 ) $this->user_dst++; 
  67. $this->posts[$row['post_id']][] = array( 'user_id' =>$row['beer_src'], 'username' =>$row['beer_poster'], 'user_level' =>$row['user_level'], 'user_jr' =>$row['user_jr'] ); 
  68. $this->post_users[$row['post_id']][] = $row['beer_src']; 
  69. if ( @!in_array($row[$row['post_id']]['beer_src'], $this->topic_replies) ) 
  70. { 
  71. $this->topic_replies[$row['post_id']][] = $row['beer_src']; 
  72. } 
  73. } 
  74. } 
  75. $this->users[$row['user_id']]['src'] = intval($row['user_beers_src']); 
  76. $this->users[$row['user_id']]['dst'] = intval($row['user_beers_dst']); 
  77. } 
  78. if ( count($trash_beers) ) 
  79. { 
  80. $sql = "delete from " . BEER_TABLE . " where beer_id IN (" . implode(',', $trash_beers) . ")"; 
  81. if ( !$db->sql_query($sql) ) 
  82. { 
  83. message_die(GENERAL_ERROR, 'Couldn't delete fake topic beers', '', __LINE__, __FILE__, $sql); 
  84. } 
  85. } 
  86. $u_sql = array(); 
  87. if ( $userdata['user_beers_src'] != $this->user_src ) 
  88. $u_sql[] = " user_beers_src = " . intval($this->user_src) . " "; 
  89. if ( $userdata['user_beers_dst'] != $this->user_dst ) 
  90. $u_sql[] = " user_beers_dst = " . intval($this->user_dst) . " "; 
  91. if ( count($u_sql) ) 
  92. { 
  93. $sql = "update " . USERS_TABLE . " set " . implode(',', $u_sql) . " where user_id = $user_id"; 
  94. if ( !$db->sql_query($sql) ) 
  95. { 
  96. message_die(GENERAL_ERROR, 'Couldn't update user topic beers info', '', __LINE__, __FILE__, $sql); 
  97. } 
  98. $userdata['user_beers_src'] = intval($this->user_src); 
  99. $userdata['user_beers_dst'] = intval($this->user_dst); 
  100. } 
  101. }
  102. ?>






Chodzi o te zapytania z Left Join, da się coś z nimi zrobić? Może jakoś zastąpić Left Join innym? 



Pozdrawiam.
Go to the top of the page
+Quote Post

Posty w temacie
- @Wu   [MySQL] "Ciężkie" zapytanie - optymalizacja?   2.08.2008, 09:23:25
- - Zbychu666   1) Mogłeś dać info że to mod do forum phpBB. 2) Mo...   2.08.2008, 11:20:18
- - @Wu   1) No fakt, jest to mod, ale głównie chodzi o jedn...   2.08.2008, 12:23:20
- - Zbychu666   Problem jest w tym kawałku zapytania, które r...   2.08.2008, 21:08:44
- - @Wu   Niestety, po takim zabiegu liczba piw (beer) wynos...   2.08.2008, 21:55:40
- - kwiateusz   te explainy i indexy to mi srednio wychodza ale mo...   2.08.2008, 22:24:18
- - osiris   Indeks na kolumnie topic_id jest juz zalozony (jak...   2.08.2008, 22:59:48
- - @Wu   Działa, tylko... Strona wygenerowana w 7.58 sekund...   3.08.2008, 07:17:05
- - osiris   wklej explainy z nowych zapytan. moze to probleme...   3.08.2008, 09:08:43
- - @Wu   Zapytanie na screenie nie jest widoczne w całości....   3.08.2008, 10:04:59
- - osiris   sprobuj zalozyc indeksy na tabeli BEER_TABLE: 1) d...   3.08.2008, 11:20:31
- - @Wu   Jak je założyć? Jakieś zapytania?   3.08.2008, 11:32:34
- - osiris   Widze ze korzystasz z phpmyadmin, wiec: - przejdz ...   3.08.2008, 11:57:06
- - @Wu   A tutaj w przypadku "oryginalnego" zapyt...   3.08.2008, 16:15:40
- - dr_bonzo   Mozesz wkleic SQL tworzacy tabele [wszystkie tu wy...   3.08.2008, 16:23:27
- - @Wu   Podać kod zapytania SQL, które tworzy tabele beers...   3.08.2008, 16:27:58
- - kwiateusz   jedna rzecz mi przychodzi na mysl wykonaj zapytan...   3.08.2008, 16:31:27
- - osiris   Hmm, ok zacznijmy od innej strony. Jakie dane ma w...   3.08.2008, 18:00:21
- - @Wu   Wydaje mi się, że liczba piw, gdyż nawet w widoku ...   3.08.2008, 18:58:19


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.08.2025 - 04:47