Guest User

Untitled

a guest
Dec 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. select urls.id as urlId,
  2. count(case when s1.hit_type = 0 then 1 end) as aCount,
  3. count(case when s1.hit_type = 1 then 1 end) as bCount,
  4. count(case when s1.hit_type = 2 then 1 end) as cCount,
  5. count(distinct s1.source_id) as sourcesCount
  6. from urls join stats s1 on urls.id = s1.url_id
  7. where s1.hit_date >= '2017-12-12'
  8. group by urls.id
  9. order by aCount desc
  10. limit 0,100;
  11.  
  12. mysql> show create table stats;
  13.  
  14. | stats | CREATE TABLE `stats` (
  15. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  16. `url_id` varchar(100) DEFAULT NULL,
  17. `hit_date` datetime DEFAULT NULL,
  18. `hit_type` tinyint(4) DEFAULT NULL,
  19. `source_id` bigint(20) unsigned DEFAULT NULL,
  20. PRIMARY KEY (`id`),
  21. KEY `url_id_idx` (`url_id`),
  22. KEY `source_id` (`source_id`),
  23. KEY `stats_hit_date_idx` (`hit_date`),
  24. CONSTRAINT `stats_ibfk_1` FOREIGN KEY (`url_id`) REFERENCES `urls` (`ID`),
  25. CONSTRAINT `stats_ibfk_2` FOREIGN KEY (`source_id`) REFERENCES `sources` (`id`)
  26. ) ENGINE=InnoDB AUTO_INCREMENT=6027557 DEFAULT CHARSET=latin1 |
  27.  
  28. mysql> describe select...
  29. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  30. +----+-------------+---------+--------+-------------------------------------------------------------------------------------------------+---------+---------+--------------------------+---------+----------------------------------------------+
  31. | 1 | SIMPLE | s1 | ALL | url_id_idx,stats_hit_date_idx | NULL | NULL | NULL | 5869695 | Using where; Using temporary; Using filesort |
  32. | 1 | SIMPLE | urls | eq_ref | PRIMARY,urls_email_idx,urls_status_idx,deptId_idx,deptId_status_email_idx | PRIMARY | 102 | db.s1.url_id | 1 | Using index |
Add Comment
Please, Sign In to add comment