2026-10-10-market-field-scope-indexes.sql 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. -- 移动市场现场 scope 查询复合索引
  2. -- MySQL 5.7;通过 information_schema + PREPARE 保证重复执行安全。
  3. SET NAMES utf8mb4;
  4. SET @db := DATABASE();
  5. SET @idx_exists := (
  6. SELECT COUNT(*) FROM information_schema.STATISTICS
  7. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_collection_region_staff'
  8. AND INDEX_NAME = 'idx_crs_market_leader_scope'
  9. );
  10. SET @sql := IF(@idx_exists = 0,
  11. 'ALTER TABLE `biz_collection_region_staff` ADD INDEX `idx_crs_market_leader_scope` (`tenant_no`,`user_id`,`is_leader`,`del_flag`,`region_id`)',
  12. 'SELECT 1');
  13. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  14. SET @idx_exists := (
  15. SELECT COUNT(*) FROM information_schema.STATISTICS
  16. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_presign_order'
  17. AND INDEX_NAME = 'idx_po_market_sales_scope'
  18. );
  19. SET @sql := IF(@idx_exists = 0,
  20. 'ALTER TABLE `biz_presign_order` ADD INDEX `idx_po_market_sales_scope` (`tenant_no`,`del_flag`,`sales_user_id`)',
  21. 'SELECT 1');
  22. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  23. SET @idx_exists := (
  24. SELECT COUNT(*) FROM information_schema.STATISTICS
  25. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_presign_order'
  26. AND INDEX_NAME = 'idx_po_market_region_scope'
  27. );
  28. SET @sql := IF(@idx_exists = 0,
  29. 'ALTER TABLE `biz_presign_order` ADD INDEX `idx_po_market_region_scope` (`tenant_no`,`del_flag`,`region_id`)',
  30. 'SELECT 1');
  31. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  32. SET @idx_exists := (
  33. SELECT COUNT(*) FROM information_schema.STATISTICS
  34. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_presign_order'
  35. AND INDEX_NAME = 'idx_po_market_manager_scope'
  36. );
  37. SET @sql := IF(@idx_exists = 0,
  38. 'ALTER TABLE `biz_presign_order` ADD INDEX `idx_po_market_manager_scope` (`tenant_no`,`del_flag`,`region_manager_user_id`)',
  39. 'SELECT 1');
  40. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  41. SET @idx_exists := (
  42. SELECT COUNT(*) FROM information_schema.STATISTICS
  43. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_protocol_collection_info'
  44. AND INDEX_NAME = 'idx_pci_market_parent_scope'
  45. );
  46. SET @sql := IF(@idx_exists = 0,
  47. 'ALTER TABLE `biz_protocol_collection_info` ADD INDEX `idx_pci_market_parent_scope` (`tenant_no`,`del_flag`,`collection_protocol_id`)',
  48. 'SELECT 1');
  49. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  50. SET @idx_exists := (
  51. SELECT COUNT(*) FROM information_schema.STATISTICS
  52. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_protocol_collection_info'
  53. AND INDEX_NAME = 'idx_pci_market_sales_scope'
  54. );
  55. SET @sql := IF(@idx_exists = 0,
  56. 'ALTER TABLE `biz_protocol_collection_info` ADD INDEX `idx_pci_market_sales_scope` (`tenant_no`,`del_flag`,`sales_user_id`,`collection_protocol_id`)',
  57. 'SELECT 1');
  58. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  59. SET @idx_exists := (
  60. SELECT COUNT(*) FROM information_schema.STATISTICS
  61. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_protocol_collection_info'
  62. AND INDEX_NAME = 'idx_pci_market_region_scope'
  63. );
  64. SET @sql := IF(@idx_exists = 0,
  65. 'ALTER TABLE `biz_protocol_collection_info` ADD INDEX `idx_pci_market_region_scope` (`tenant_no`,`del_flag`,`region_id`,`collection_protocol_id`)',
  66. 'SELECT 1');
  67. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  68. SET @idx_exists := (
  69. SELECT COUNT(*) FROM information_schema.STATISTICS
  70. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_protocol_collection_info'
  71. AND INDEX_NAME = 'idx_pci_market_manager_scope'
  72. );
  73. SET @sql := IF(@idx_exists = 0,
  74. 'ALTER TABLE `biz_protocol_collection_info` ADD INDEX `idx_pci_market_manager_scope` (`tenant_no`,`del_flag`,`region_manager_user_id`,`collection_protocol_id`)',
  75. 'SELECT 1');
  76. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  77. SET @idx_exists := (
  78. SELECT COUNT(*) FROM information_schema.STATISTICS
  79. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_research_donation_collection'
  80. AND INDEX_NAME = 'idx_rdc_market_parent_scope'
  81. );
  82. SET @sql := IF(@idx_exists = 0,
  83. 'ALTER TABLE `biz_research_donation_collection` ADD INDEX `idx_rdc_market_parent_scope` (`tenant_no`,`del_flag`,`research_donation_id`)',
  84. 'SELECT 1');
  85. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  86. SET @idx_exists := (
  87. SELECT COUNT(*) FROM information_schema.STATISTICS
  88. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_research_donation_collection'
  89. AND INDEX_NAME = 'idx_rdc_market_sales_scope'
  90. );
  91. SET @sql := IF(@idx_exists = 0,
  92. 'ALTER TABLE `biz_research_donation_collection` ADD INDEX `idx_rdc_market_sales_scope` (`tenant_no`,`del_flag`,`sales_user_id`,`research_donation_id`)',
  93. 'SELECT 1');
  94. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  95. SET @idx_exists := (
  96. SELECT COUNT(*) FROM information_schema.STATISTICS
  97. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_research_donation_collection'
  98. AND INDEX_NAME = 'idx_rdc_market_region_scope'
  99. );
  100. SET @sql := IF(@idx_exists = 0,
  101. 'ALTER TABLE `biz_research_donation_collection` ADD INDEX `idx_rdc_market_region_scope` (`tenant_no`,`del_flag`,`region_id`,`research_donation_id`)',
  102. 'SELECT 1');
  103. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  104. SET @idx_exists := (
  105. SELECT COUNT(*) FROM information_schema.STATISTICS
  106. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'biz_research_donation_collection'
  107. AND INDEX_NAME = 'idx_rdc_market_manager_scope'
  108. );
  109. SET @sql := IF(@idx_exists = 0,
  110. 'ALTER TABLE `biz_research_donation_collection` ADD INDEX `idx_rdc_market_manager_scope` (`tenant_no`,`del_flag`,`region_manager_user_id`,`research_donation_id`)',
  111. 'SELECT 1');
  112. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;