<select id="commonSearch" resultType="com.ccc.interfaces.web.vo.DocumentSearchVo">
select
di.id,
di.name,
di.type,
di.document_category_id,
dc.name documentCategoryName,
di.ascription_id,
di.attachment_type,
di.urls,
di.update_time
from
document_info di
left join
document_category dc on
di.document_category_id = dc.id
<where>
<if test="searchDto.type != null">
and di.type = #{searchDto.type}
</if>
<if test="searchDto.condition != null and searchDto.condition != ''">
AND (LOWER(di.name) LIKE CONCAT('%', LOWER(#{searchDto.condition}), '%')
OR
LOWER(dc.name) LIKE CONCAT('%', LOWER(#{searchDto.condition}), '%'))
</if>
</where>
ORDER BY di.update_time DESC
</select>
使用LOWER()函数可实现。
索引优化:
如果使用 LOWER(),建议创建函数索引:
sql
CREATE INDEX idx_di_name_lower ON document_info (LOWER(name));
CREATE INDEX idx_dc_name_lower ON document_category (LOWER(name));
如果使用 ILIKE,可尝试使用 text_pattern_ops 运算符类索引:
sql
CREATE INDEX idx_di_name_ilike ON document_info (name text_pattern_ops);
字符集兼容性:
ILIKE 默认依赖数据库的排序规则(collation),若需特殊字符(如中文)支持,需确保数据库使用 UTF8 编码。