Advertisement
acritox

librenms: MySQL index for table notifications_attribs

Jul 5th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. MySQL slowlog (Server version: 5.7.26 MySQL Community Server):
  2. # User@Host: librenms[librenms] @ localhost [] Id: 19228
  3. # Query_time: 6.830290
  4. # Lock_time: 0.000043
  5. # Rows_sent: 1
  6. # Rows_examined: 23752917
  7. select count(*) as aggregate from `notifications` left join `notifications_attribs` on `notifications_attribs`.`notifications_id` = `notifications`.`notifications_id` where (`notifications_attribs`.`key` = 'sticky' and `notifications_attribs`.`value` = 1) or (not exists (select 1 from `notifications_attribs` where notifications.notifications_id = notifications_attribs.notifications_id and `notifications_attribs`.`user_id` = 123));
  8.  
  9. mysql> select count(*) from notifications;
  10. +----------+
  11. | COUNT(*) |
  12. +----------+
  13. | 52 |
  14. +----------+
  15. 1 row in set (0.00 sec)
  16.  
  17. mysql> select count(*) from notifications_attribs;
  18. +----------+
  19. | COUNT(*) |
  20. +----------+
  21. | 893 |
  22. +----------+
  23. 1 row in set (0.00 sec)
  24.  
  25. mysql> select count(1) as aggregate from `notifications` left join `notifications_attribs` on `notifications_attribs`.`notifications_id` = `notifications`.`notifications_id` where (`notifications_attribs`.`key` = 'sticky' and `notifications_attribs`.`value` = 1) or (not exists (select 1 from `notifications_attribs` where notifications.notifications_id = notifications_attribs.notifications_id and `notifications_attribs`.`user_id` = 123) );
  26. +-----------+
  27. | aggregate |
  28. +-----------+
  29. | 0 |
  30. +-----------+
  31. 1 row in set (7.30 sec)
  32.  
  33. mysql> explain extended select count(*) as aggregate from `notifications` left join `notifications_attribs` on `notifications_attribs`.`notifications_id` = `notifications`.`notifications_id` where (`notifications_attribs`.`key` = 'sticky' and `notifications_attribs`.`value` = 1) or (not exists (select 1 from `notifications_attribs` where notifications.notifications_id = notifications_attribs.notifications_id and `notifications_attribs`.`user_id` = 123));
  34. +----+--------------------+-----------------------+------------+-------+---------------+------------------------------+---------+------+------+----------+----------------------------------------------------+
  35. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  36. +----+--------------------+-----------------------+------------+-------+---------------+------------------------------+---------+------+------+----------+----------------------------------------------------+
  37. | 1 | PRIMARY | notifications | NULL | index | NULL | notifications_severity_index | 5 | NULL | 51 | 100.00 | Using index |
  38. | 1 | PRIMARY | notifications_attribs | NULL | ALL | NULL | NULL | NULL | NULL | 790 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
  39. | 2 | DEPENDENT SUBQUERY | notifications_attribs | NULL | ALL | NULL | NULL | NULL | NULL | 790 | 1.00 | Using where |
  40. +----+--------------------+-----------------------+------------+-------+---------------+------------------------------+---------+------+------+----------+----------------------------------------------------+
  41. 3 rows in set, 3 warnings (0.00 sec)
  42.  
  43. mysql> show indexes from notifications_attribs;
  44. +-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  45. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  46. +-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  47. | notifications_attribs | 0 | PRIMARY | 1 | attrib_id | A | 790 | NULL | NULL | | BTREE | | |
  48. +-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  49. 1 row in set (0.00 sec)
  50.  
  51. mysql> CREATE INDEX `notifications_attribs_user_idx` ON `notifications_attribs` (`notifications_id`,`user_id`);
  52. Query OK, 0 rows affected (0.03 sec)
  53. Records: 0 Duplicates: 0 Warnings: 0
  54.  
  55. mysql> show indexes from notifications_attribs;
  56. +-----------------------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  57. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  58. +-----------------------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  59. | notifications_attribs | 0 | PRIMARY | 1 | attrib_id | A | 790 | NULL | NULL | | BTREE | | |
  60. | notifications_attribs | 1 | notifications_attribs_user_idx | 1 | notifications_id | A | 51 | NULL | NULL | | BTREE | | |
  61. | notifications_attribs | 1 | notifications_attribs_user_idx | 2 | user_id | A | 790 | NULL | NULL | | BTREE | | |
  62. +-----------------------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  63. 3 rows in set (0.00 sec)
  64.  
  65. mysql> explain extended select count(*) as aggregate from `notifications` left join `notifications_attribs` on `notifications_attribs`.`notifications_id` = `notifications`.`notifications_id` where (`notifications_attribs`.`key` = 'sticky' and `notifications_attribs`.`value` = 1) or (not exists (select 1 from `notifications_attribs` where notifications.notifications_id = notifications_attribs.notifications_id and `notifications_attribs`.`user_id` = 123));
  66. +----+--------------------+-----------------------+------------+-------+--------------------------------+--------------------------------+---------+-----------------------------------------------+------+----------+-------------+
  67. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  68. +----+--------------------+-----------------------+------------+-------+--------------------------------+--------------------------------+---------+-----------------------------------------------+------+----------+-------------+
  69. | 1 | PRIMARY | notifications | NULL | index | NULL | notifications_severity_index | 5 | NULL | 51 | 100.00 | Using index |
  70. | 1 | PRIMARY | notifications_attribs | NULL | ref | notifications_attribs_user_idx | notifications_attribs_user_idx | 4 | librenms.notifications.notifications_id | 15 | 100.00 | Using where |
  71. | 2 | DEPENDENT SUBQUERY | notifications_attribs | NULL | ref | notifications_attribs_user_idx | notifications_attribs_user_idx | 8 | librenms.notifications.notifications_id,const | 1 | 100.00 | Using index |
  72. +----+--------------------+-----------------------+------------+-------+--------------------------------+--------------------------------+---------+-----------------------------------------------+------+----------+-------------+
  73. 3 rows in set, 3 warnings (0.00 sec)
  74.  
  75. mysql> select count(1) as aggregate from `notifications` left join `notifications_attribs` on `notifications_attribs`.`notifications_id` = `notifications`.`notifications_id` where (`notifications_attribs`.`key` = 'sticky' and `notifications_attribs`.`value` = 1) or (not exists (select 1 from `notifications_attribs` where notifications.notifications_id = notifications_attribs.notifications_id and `notifications_attribs`.`user_id` = 123) );
  76. +-----------+
  77. | aggregate |
  78. +-----------+
  79. | 0 |
  80. +-----------+
  81. 1 row in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement