Guest User

SQL

a guest
Jan 13th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 8.84 KB | None | 0 0
  1. DROP DATABASE tag_test;
  2. CREATE DATABASE tag_test;
  3. USE tag_test;
  4.  
  5. CREATE TABLE tag(
  6. id INT PRIMARY KEY AUTO_INCREMENT,
  7. description VARCHAR(1000));
  8.  
  9. CREATE TABLE content(
  10. id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  11. description VARCHAR(1000));
  12.  
  13. CREATE TABLE tag_content_rel(
  14. tag_id INT NOT NULL,
  15. content_id INT NOT NULL);
  16.  
  17. CREATE INDEX tag_content_rel_index ON tag_content_rel(tag_id, content_id);
  18. /*
  19. CREATE INDEX tag_index ON tag_content_rel(tag_id);
  20. CREATE INDEX content_index ON tag_content_rel(content_id);
  21. */
  22.  
  23. DELIMITER $$
  24. CREATE PROCEDURE `add_tag`(IN n INT)
  25. BEGIN
  26.     DECLARE i INT UNSIGNED DEFAULT 0;
  27.     WHILE i < n DO
  28.         INSERT INTO `tag`()
  29.         VALUES
  30.         -- 一次插入多行可以显著加快插入速度。
  31.         (),(),(),(),(),(),(),(),(),(),
  32.         (),(),(),(),(),(),(),(),(),(),
  33.         (),(),(),(),(),(),(),(),(),(),
  34.         (),(),(),(),(),(),(),(),(),(),
  35.         (),(),(),(),(),(),(),(),(),(),
  36.         (),(),(),(),(),(),(),(),(),(),
  37.         (),(),(),(),(),(),(),(),(),(),
  38.         (),(),(),(),(),(),(),(),(),(),
  39.         (),(),(),(),(),(),(),(),(),(),
  40.         (),(),(),(),(),(),(),(),(),();
  41.         SET i = i + 1;
  42.     END WHILE;
  43. END $$
  44. DELIMITER ;
  45.  
  46. DELIMITER $$
  47. CREATE PROCEDURE `add_content`(IN n INT)
  48. BEGIN
  49.     DECLARE i INT UNSIGNED DEFAULT 0;
  50.     WHILE i < n DO
  51.         INSERT INTO `content`()
  52.         VALUES
  53.         (),(),(),(),(),(),(),(),(),(),
  54.         (),(),(),(),(),(),(),(),(),(),
  55.         (),(),(),(),(),(),(),(),(),(),
  56.         (),(),(),(),(),(),(),(),(),(),
  57.         (),(),(),(),(),(),(),(),(),(),
  58.         (),(),(),(),(),(),(),(),(),(),
  59.         (),(),(),(),(),(),(),(),(),(),
  60.         (),(),(),(),(),(),(),(),(),(),
  61.         (),(),(),(),(),(),(),(),(),(),
  62.         (),(),(),(),(),(),(),(),(),();
  63.         SET i = i + 1;
  64.     END WHILE;
  65. END $$
  66. DELIMITER ;
  67.  
  68. DELIMITER $$
  69. CREATE PROCEDURE `add_tag_content_rel`(IN n INT, IN tag_count INT, IN content_count INT)
  70. BEGIN
  71.     DECLARE i INT UNSIGNED DEFAULT 0;
  72.     WHILE i < n DO
  73.         INSERT INTO `tag_content_rel`(tag_id, content_id)
  74.         VALUES
  75.         -- 为了不学 MySQL 的过程的语法所以才复制粘贴这么多次。
  76.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  77.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  78.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  79.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  80.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  81.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  82.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  83.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  84.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),
  85.         (FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count)),(FLOOR(RAND()*tag_count), FLOOR(RAND()*content_count));
  86.         SET i = i + 1;
  87.     END WHILE;
  88. END $$
  89. DELIMITER ;
  90.  
  91. CALL add_tag(1000);
  92. CALL add_content(1000);
  93. CALL add_tag_content_rel(100000, 100000, 100000);
  94.  
  95. -- 耗时 00:04:40.9
  96.  
  97. -- 使用下面的语句获取某个 content 的所有 tag:
  98. USE tag_test;
  99.  
  100. SELECT *
  101. FROM tag_content_rel
  102. WHERE content_id = 50000;
  103.  
  104. -- 然后使用上面那条语句获取的 tag_id 修改下面的语句:
  105.  
  106. USE tag_test;
  107.  
  108. SELECT content_id, COUNT(*)
  109. FROM tag_content_rel
  110. WHERE tag_id IN (
  111. 730,2621,2805,3200,3340,3590,3969,4039,4799,5249,8859,11894,12628,12646,16959,17024,17142,18032,18861,19316,20839,21179,22346,22507,22522,22639,23562,23822,25172,25786,25821,26606,29899,29917,30586,30901,31216,31413,32562,32567,34740,36586,36954,38109,39202,40519,40756,40816,41464,42942,43069,43286,43344,44787,44950,45549,45652,46313,47111,50549,51942,52738,52959,52961,54034,55526,59162,59767,59945,60361,60816,61307,61730,62269,62503,62589,63960,64580,64634,64794,65209,66332,68222,69396,69905,70629,70939,71277,71804,72580,72896,73651,74301,74525,74706,75153,76169,76500,78042,78148,79109,81463,82140,84217,85212,85327,85584,86392,86908,88188,88475,89175,89190,91156,93202,94124,95294,95345,96013,98135,99679
  112. )
  113. GROUP BY content_id
  114. ORDER BY COUNT(*) DESC;
  115.  
  116. /*
  117. EXPLAIN 结果:
  118. "id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "Extra"
  119. "1" "SIMPLE"    "tag_content_rel"   "index" "tag_content_rel_index" "tag_content_rel_index" "8" \N  "1" "Using where; Using index; Using temporary; Using filesort"
  120. */
Advertisement
Add Comment
Please, Sign In to add comment