Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Optymalizacja zapytania
wlamywacz
post 16.06.2010, 14:06:36
Post #1





Grupa: Zarejestrowani
Postów: 535
Pomógł: 27
Dołączył: 3.05.2005

Ostrzeżenie: (20%)
X----


Witam

Struktura tabel:
  1. CREATE TABLE IF NOT EXISTS `pages` (
  2. `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `url` varchar(200) NOT NULL,
  4. `lastpr` smallint(2) NOT NULL,
  5. `pr` smallint(2) NOT NULL,
  6. `updateip` tinyint(1) UNSIGNED NOT NULL,
  7. `ip` bigint(20) UNSIGNED NOT NULL,
  8. `categoryid` int(11) UNSIGNED NOT NULL,
  9. `lastcheck` datetime NOT NULL,
  10. `updatepr` tinyint(1) UNSIGNED NOT NULL,
  11. `googlelastupdatesite` datetime NOT NULL,
  12. `googleupdatesite` tinyint(1) UNSIGNED NOT NULL,
  13. `googlesite` bigint(20) UNSIGNED NOT NULL,
  14. `yahoolastupdatesite` datetime NOT NULL,
  15. `yahooupdatesite` tinyint(1) UNSIGNED NOT NULL,
  16. `yahoosite` bigint(20) UNSIGNED NOT NULL,
  17. `yahoolastupdatebl` datetime NOT NULL,
  18. `yahooupdatebl` tinyint(1) UNSIGNED NOT NULL,
  19. `yahoobl` bigint(20) UNSIGNED NOT NULL,
  20. `information` text NOT NULL,
  21. `ping` int(11) NOT NULL,
  22. `lastping` datetime NOT NULL,
  23. PRIMARY KEY (`id`),
  24. UNIQUE KEY `id` (`id`)
  25. )

  1. CREATE TABLE IF NOT EXISTS `status` (
  2. `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `pageid` bigint(20) UNSIGNED NOT NULL,
  4. `date` datetime NOT NULL,
  5. `server` tinyint(1) NOT NULL,
  6. `php` tinyint(1) NOT NULL,
  7. UNIQUE KEY `id` (`id`),
  8. KEY `pageid` (`pageid`,`date`)
  9. )

  1.  
  2. CREATE TABLE IF NOT EXISTS `categories` (
  3. `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  4. `name` varchar(80) NOT NULL,
  5. PRIMARY KEY (`id`),
  6. UNIQUE KEY `id` (`id`)
  7. )

Zapytanie:
  1. SELECT p.`id`, p.`url`, p.`ip`, p.`lastpr`, p.`pr`, p.`googlesite`, p.`yahoosite`, p.`yahoobl`, p.`ping`, p.`information`, p.`categoryid` AS `category`, c.`name` AS `categoryname`, s.`server`, s.`php`, INET_NTOA(p.`ip`) AS `ip`, (SELECT COUNT(st.`id`) FROM `status` AS `st` WHERE st.`pageid` = p.`id` AND (st.`server` = 0 OR st.`php` = 0) AND DATE_ADD(st.`date`, INTERVAL 7 DAY) > NOW()) AS `errors` FROM `pages` AS `p` LEFT JOIN `categories` AS `c` ON c.`id` = p.`categoryid` LEFT JOIN `status` AS `s` ON (s.`id` = (SELECT `id` FROM `status` WHERE `pageid` = p.id ORDER BY `date` DESC LIMIT 1))


Niestety w tabeli status ilość rekordów wynosi 500 tysięcy do miliona może nawet kilku w ekstremalnych przypadkach. Całe zapytanie wykonuje się około 2,5 sekundy przy 400k rekordów. Wąskimi gardłami w tym zapytaniu są te dwa pod zapytania jednak niezbyt mam pojęcie jak się ich pozbyć i zastąpić czymś bardziej optymalnym. Z góry dziękuje za pomoc.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 07:55