日期:2014-05-18  浏览次数:20484 次

有一个SQL语句需要优化,高手帮帮忙
是分词后的查询,数据一多久很慢,这样的查询2000条数据要2秒多才能出结果,求优化,谢谢

SQL code

SELECT 
            dbo.userlib.u_id, 
            dbo.Company.C_company, 
            DATEDIFF(day, dbo.userlib.U_regdate, GETDATE()) AS years, 
            dbo.userlib.U_level, 
            Keyword = CASE WHEN dbo.Company.c_Keyword like '本机地址' 
                                or dbo.Company.c_Keyword like '%[,]本机地址[,]%'
                                or dbo.Company.c_Keyword like '%[,]本机地址'
                                or dbo.Company.c_Keyword like '本机地址' THEN 1
                        ELSE 0 END,
            CASE WHEN u_level = 3 THEN 3 
                    WHEN u_level = 2 THEN 3 
                    WHEN u_level = 1 THEN 2
                    ELSE 0 END AS vip_level,
            c_keyword = CASE WHEN dbo.company.c_Keyword IS NULL THEN ''''
                    ELSE dbo.company.c_Keyword END,
            dbo.Company.c_hits,
            dbo.company.c_address,
            dbo.company.c_tel,
            dbo.company.c_a_id,
            dbo.company.c_c_id,
            dbo.company.c_cy_id,
            dbo.company.c_description,
            c_rtime = case when dbo.company.c_RefreshTime < getdate() 
                                or dbo.Company.c_Keyword not like '本机地址' 
                                and dbo.Company.c_Keyword not like '%[,]本机地址[,]%' 
                                and dbo.Company.c_Keyword not like '%[,]本机地址'
                                and dbo.Company.c_Keyword not like '本机地址[,]%' then '1987-08-28'
                        else dbo.company.c_RefreshTime end
        FROM         
            dbo.Company INNER JOIN
            dbo.userlib ON dbo.Company.C_u_id = dbo.userlib.u_id
        WHERE userlib.u_yes = 1 AND  c_company like '%本机地址%' or c_address like '%本机地址%' or c_tel like '%本机地址%' or  c_company like '%地址%' or c_address like '%地址%' or c_tel like '%地址%' or  c_company like '%本机%' or c_address like '%本机%' or c_tel like '%本机%' or  c_company like '%地%' or c_address like '%地%' or c_tel like '%地%' or  c_company like '%址%' or c_address like '%址%' or c_tel like '%址%' or  c_company like '%本%' or c_address like '%本%' or c_tel like '%本%' or  c_company like '%机%' or c_address like '%机%' or c_tel like '%机%' or c_keyword like '本机地址' or c_keyword like '%[,]本机地址[,]%' or c_keyword like '%[,]本机地址' or c_keyword like '本机地址[,]%'



------解决方案--------------------
你这么多or条件,基本上使用的是like,快不起来的.
------解决方案--------------------
都一些LIKE 语句没啥好办法。全文索引吧

你AND 与OR的地方都不打括号的?
------解决方案--------------------
模糊查询,不好优化耶.

------解决方案--------------------
建议引入视图,然后从视图中查询.视图是预编译的,能好一点.