Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. CREATE TABLE `tempdata` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  4. `title` varchar(20) DEFAULT NULL,
  5. PRIMARY KEY (`id`),
  6. KEY `tempdata_created_at` (`created_at`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=58511 DEFAULT CHARSET=utf8;
  8.  
  9.  
  10. EXPLAIN EXTENDED SELECT MONTH(created_at) AS MONTH, COUNT(*) AS COUNT
  11. FROM tempdata
  12. WHERE YEAR(created_at)=2016
  13. GROUP BY MONTH(created_at);
  14.  
  15. EXPLAIN EXTENDED SELECT
  16. SUM(CASE Month(created_at) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
  17. SUM(CASE month(created_at) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
  18. SUM(CASE month(created_at) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
  19. SUM(CASE month(created_at) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
  20. SUM(CASE month(created_at) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
  21. SUM(CASE month(created_at) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
  22. SUM(CASE month(created_at) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
  23. SUM(CASE month(created_at) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
  24. SUM(CASE month(created_at) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
  25. SUM(CASE month(created_at) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
  26. SUM(CASE month(created_at) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
  27. SUM(CASE month(created_at) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
  28. SUM(CASE year(created_at) WHEN 2016 THEN 1 ELSE 0 END) AS 'TOTAL'
  29. FROM
  30. tempdata
  31. WHERE
  32. created_at BETWEEN '2016/01/01' AND '2016/12/31'
  33.  
  34.  
  35.  
  36. 1 SIMPLE tempdata index tempdata_created_at tempdata_created_at 5 57257 100.00 Using where; Using index; Using temporary; Using filesort
  37.  
  38. 1 SIMPLE tempdata range tempdata_created_at tempdata_created_at 5 28628 100.00 Using where; Using index
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement