-- 为社区帖子表添加全文索引，优化搜索性能
-- 执行前请确保 MySQL 版本 >= 5.7（支持中文全文索引）

-- 检查索引是否已存在，如果不存在则创建
SET @index_exists = (
    SELECT COUNT(*) 
    FROM information_schema.statistics 
    WHERE table_schema = DATABASE() 
    AND table_name = 'community_post' 
    AND index_name = 'ft_title_content'
);

SET @sql = IF(@index_exists = 0,
    'CREATE FULLTEXT INDEX ft_title_content ON community_post(title, content)',
    'SELECT ''Index ft_title_content already exists, skipping creation'' AS message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 验证索引创建结果
SELECT 
    TABLE_NAME,
    INDEX_NAME,
    COLUMN_NAME,
    INDEX_TYPE
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'community_post'
AND index_name = 'ft_title_content';

