性能优化 #

一、索引优化 #

1.1 Schema设计优化 #

使用DocValues

xml
<field name="price" type="pdouble" indexed="true" stored="true" docValues="true"/>
<field name="category" type="string" indexed="true" stored="true" docValues="true"/>

减少stored字段

xml
<!-- 仅搜索不展示 -->
<field name="search_text" type="text_general" indexed="true" stored="false"/>

合理使用multiValued

xml
<field name="tags" type="string" indexed="true" stored="true" multiValued="true"/>

1.2 索引配置优化 #

solrconfig.xml

xml
<indexConfig>
  <!-- 增大缓冲区 -->
  <ramBufferSizeMB>256</ramBufferSizeMB>
  
  <!-- 禁用复合文件 -->
  <useCompoundFile>false</useCompoundFile>
  
  <!-- 合并策略 -->
  <mergePolicyFactory class="org.apache.solr.index.TieredMergePolicyFactory">
    <int name="maxMergeAtOnce">10</int>
    <int name="segmentsPerTier">10</int>
  </mergePolicyFactory>
  
  <!-- 合并调度器 -->
  <mergeScheduler class="org.apache.lucene.index.ConcurrentMergeScheduler">
    <int name="maxThreadCount">4</int>
    <int name="maxMergeCount">8</int>
  </mergeScheduler>
</indexConfig>

1.3 提交策略优化 #

xml
<updateHandler class="solr.DirectUpdateHandler2">
  <!-- 硬提交 -->
  <autoCommit>
    <maxTime>15000</maxTime>
    <openSearcher>false</openSearcher>
  </autoCommit>
  
  <!-- 软提交 -->
  <autoSoftCommit>
    <maxTime>1000</maxTime>
  </autoSoftCommit>
  
  <!-- 事务日志 -->
  <updateLog>
    <str name="dir">${solr.ulog.dir:}</str>
    <int name="numRecordsToKeep">100</int>
  </updateLog>
</updateHandler>

1.4 批量索引优化 #

bash
# 使用批量索引
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '[...100文档...]'

# 延迟提交
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs?commitWithin=10000" \
  -H "Content-Type: application/json" \
  -d @large_batch.json

二、查询优化 #

2.1 使用fq缓存 #

bash
# fq会被缓存
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fq=category:tech"

2.2 限制返回字段 #

bash
# 只返回需要的字段
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title,price"

2.3 使用filter代替must #

bash
# 不推荐
curl "http://localhost:8983/solr/mycore/select?q={!bool must='title:Solr' must='category:tech'}"

# 推荐
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fq=category:tech"

2.4 避免深度分页 #

bash
# 不推荐
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=100000&rows=10"

# 推荐
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&rows=10&cursorMark=*"

2.5 Facet优化 #

bash
# 限制Facet数量
curl "http://localhost:8983/solr/mycore/select?q=*:*&facet=true&facet.field=category&facet.limit=10"

# 使用DocValues
curl "http://localhost:8983/solr/mycore/select?q=*:*&facet=true&facet.field=category&facet.method=enum"

三、缓存配置 #

3.1 查询缓存 #

xml
<query>
  <!-- 查询结果缓存 -->
  <queryResultCache class="solr.LRUCache" size="512" initialSize="512" autowarmCount="128"/>
  
  <!-- 过滤器缓存 -->
  <filterCache class="solr.FastLRUCache" size="512" initialSize="512" autowarmCount="128"/>
  
  <!-- 文档缓存 -->
  <documentCache class="solr.LRUCache" size="512" initialSize="512" autowarmCount="0"/>
  
  <!-- 字段值缓存 -->
  <fieldValueCache class="solr.FastLRUCache" size="512" autowarmCount="128"/>
</query>

3.2 缓存参数说明 #

参数 说明
size 最大缓存数量
initialSize 初始大小
autowarmCount 预热数量

3.3 缓存命中监控 #

bash
curl "http://localhost:8983/solr/mycore/admin/stats?key=CACHE"

四、JVM优化 #

4.1 内存配置 #

bash
# 设置堆内存
SOLR_JAVA_MEM="-Xms4g -Xmx8g" bin/solr start

# 或在solr.in.sh中配置
SOLR_JAVA_MEM="-Xms4g -Xmx8g"

4.2 GC配置 #

bash
# 使用G1垃圾收集器
GC_LOG_OPTS="-XX:+UseG1GC"

# 或在solr.in.sh中配置
GC_LOG_OPTS="-XX:+UseG1GC -XX:MaxGCPauseMillis=200"

4.3 JVM参数 #

bash
SOLR_OPTS="-XX:+UseG1GC \
           -XX:MaxGCPauseMillis=200 \
           -XX:InitiatingHeapOccupancyPercent=45 \
           -XX:+ParallelRefProcEnabled \
           -XX:+AlwaysPreTouch"

五、硬件优化 #

5.1 内存 #

  • 堆内存:4-8GB起步
  • 系统内存:至少16GB
  • 索引缓存:预留足够内存

5.2 磁盘 #

  • 使用SSD
  • RAID配置
  • 预留足够空间

5.3 CPU #

  • 多核CPU
  • 合理配置线程数

六、SolrCloud优化 #

6.1 分片规划 #

text
分片数量 = 数据量 / 单分片容量
单分片容量建议:10-50GB

6.2 副本配置 #

text
副本数量 = 2 或 3
确保高可用

6.3 ZooKeeper优化 #

bash
# zkTickTime
ZK_TICK_TIME=2000

# zkInitLimit
ZK_INIT_LIMIT=10

# zkSyncLimit
ZK_SYNC_LIMIT=5

七、监控与诊断 #

7.1 性能监控 #

bash
# 查看统计信息
curl "http://localhost:8983/solr/mycore/admin/stats"

# 查看缓存统计
curl "http://localhost:8983/solr/mycore/admin/stats?key=CACHE"

# 查看查询统计
curl "http://localhost:8983/solr/mycore/admin/stats?key=QUERYHANDLER"

7.2 慢查询分析 #

bash
# 开启调试
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&debugQuery=true&debug=timing"

7.3 索引分析 #

bash
# 查看索引大小
du -sh server/solr/mycore/data/index/

# 查看段信息
curl "http://localhost:8983/solr/mycore/admin/segments"

八、优化检查清单 #

8.1 Schema优化 #

  • [ ] 使用DocValues
  • [ ] 减少stored字段
  • [ ] 合理设置字段类型
  • [ ] 使用动态字段

8.2 索引优化 #

  • [ ] 批量索引
  • [ ] 合理提交策略
  • [ ] 定期优化索引

8.3 查询优化 #

  • [ ] 使用fq缓存
  • [ ] 限制返回字段
  • [ ] 避免深度分页
  • [ ] 使用filter

8.4 缓存优化 #

  • [ ] 配置查询缓存
  • [ ] 配置过滤器缓存
  • [ ] 监控缓存命中率

8.5 JVM优化 #

  • [ ] 合理设置堆内存
  • [ ] 选择合适的GC
  • [ ] 监控GC日志

九、总结 #

性能优化要点:

类别 要点
Schema DocValues、减少stored
索引 批量索引、提交策略
查询 fq缓存、限制字段
缓存 配置缓存、监控命中
JVM 堆内存、GC配置

最佳实践:

  • 从Schema设计开始优化
  • 使用缓存提升查询性能
  • 监控并分析性能瓶颈
  • 持续优化和调整

下一步,让我们学习运维管理!

最后更新:2026-03-27