正排索引1W条倒排索引600W条
批量插入(batch insert)批量插入(batch insert)
for(…){ //循环600W次
insert into 表(…)values(…);
}
for(…){ //循环6000次
这种插入的性能更好
insert into表(…)values(…),(…),(…),(…(…(.);
一
一次插入1000条数据
1.修改表结构两张表解决问题∶
正排索引表(docid-pk、title、url、content)
整体数量级不大,只有1W条,但是每一条比较大(content 大)
批量插入的时候,每次记录不用太多(每次插入10条)倒排索引表(id-pk、word、docid、weight)
整体数量级较大,有600W条,每一条的记录比较小
批量插入的是时候,每次记录多插入一些(每次插入1W条)2.docid的生成方式做修改
ArrayList的尾插过程中的size()作为docid(我们手动控制自增id)改成
利用mysql的表中的自增机制,作为docid 3.把保存索引数据的过程改成批量插入
4.整体过程不会变,但可能把一些不是很合理(比较冗余,或者代码位置不合适的类)进行调整
main】 c.p.searcher.indexer.command.Indexer ∶扫描目录结束,一共得到10460个文件。main】 c.p.searcher.indexer.command.Indexer ∶构建文档完毕,一共10460篇文档
2022-08-05 09:30:29.243 DEBUG 23272 —[2022-08-05 09:31:36.233 DEBUG 23272 —[
// 首先去掉<script …>…
//这两个操作会比较慢//这两个操作会比较慢
∶(m wirk wordyuos/>【>v】<【<v】ndyuos>m)177】vaop?dəut = aut1 //去掉标签
line = line.replaceAll(“<[^>]>“,” ");//多带的空格的意思是,把换行符也视为空格了
2022-08-05 09:34:38.149 DEBUG 8100 ----2022-08-05 09:34:49.518 DEBUG8100—
main] c.p.searcher.indexer.command.Indexer main] c.p.searcher.indexer.command.Indexer
2022-08-05 09:36:49.656 DEBUG 26408 — [2022-08-05 09:37:18.293 DEBUG 26408 — [
main] c.p.searcher.indexer.command.Indexer main] c.p.searcher.indexer.command.Indexer
MySQL批量插入语法∶
insert into forward_indexes (title, url, content) values
(1),‘2’,‘3’), (4’,‘5’,‘6’), (7),‘8’,‘9’);
使用mybatis的动态SQL特性
https://mybatis.org/mybatis-3/zh/dynamic-sql.html
动态 SQL的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。比如∶SELECTFROM POST P
遍历collection=“list”,
<foreach item=“item” index=“index” collection=“list’
open=“ID in(” separator=”,” close=“)” nullable=“true”>#{item}
其中,下标保存在 index (index = “index”)其中,遍历时的每一项保存在item(item = “item”)
application.yml
mybatis:
AG
classpath:mapper/index-mapper.xml
mapper-locations
AL
在 Spring 的配置文件中,指定 mybatis 查找 mapper xml 文件的路径
classpath∶就代表从 src/main/resources 下进行查找 【这个实际上是错误的理解,但现在这么理解关系不大】完整路径∶ src/main/resources/mapper/index-mapper.xml
index-mapper.xml ×
1
PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
m D
‘com.peixinchen.searcher.indexer.mapper.IndexDatabaseMapper’
<mapper namespace=
eMapper.java x
I IndexDatab
com.peixinchen.searcher.indexer.mapper;package
1
礼
import com.peixinchen.searcher.indexer.model.Document;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;
3
com.peixinchen.searcher.indexer.mapper.IndexDatabaseMapper
又5 0 7 00 9 10 1 //
import java.util.List;
@Repository // 注册 Spring bean @Mapper
梦福的
12 M
IndexDatabaseMapper
public interfac
void batchInsertForwardIndexes(@Param(“list”)List documentList);
13 14 A
index-mapper.xml
<mapper namespace=“com.peixinchen.searcher.indexer.mapper.IndexDatabaseMapper”))
“batchInsertForwardIndexes”
<insert id
这个insert标签
是为接口中的
batchlnsertForwardlndexes方法服务g bean
@Repository // 注册 Spri
// 是一个Mybatis 管理的Mapper
@Mapper public
nterface TndexdatabaseMapper {batchInsertForwardIndexes voi
@Param(“list”)List documentList);
1
<insert id=“batchInsertForwardIndexes
insert into forward_indexes(title, url, content)values <!–一共有多少条记录,<foreach collection=11ist”
采用动态 SQL
得根据用户
数来决定separato
所以这里’doc 4
tem
(#{doc.title},#{doc\url},#{doc.comtemt
@Param(“list”)
void batchInsertForwardIndexes
ist documentList);
insertinto forwardindexes(title,url,contentvalues(,??),(,?,3),(,?,?),?,?,?),?,?,?),(,?,3),(?,?),?,?,?),?,??),(,?.3)
Parameters:
0(String), 0(String), 0(String), 1(String), 1(String), 1(String), 2(String), 2(String), 2(String), 3(String), 3(String), 3(String), 4(String), 4(String), 4(String),
5(String),5(String),5(String),6(String),6(String),6(String),6(String),7(String),7(String),7(String),8(String),8(String),8(String), 9(String), 8(String), 9(String), 9(String)
“docid”>‘docId’
<insert id=“batchInsertForwardIndexes” useGeneratedKeys=“true” keyProperty
需要把插入后的自增id的值填回我们的Document对象中
@S1f4j @Data
public class Document
List SubList(int fromIndex,
eturn contentBuilder.tostring()
.replaceAll( regex: “<script[>]*>[<]", replacement:.replaceAll( regex: "<[^>]>”, replacement:" “).replaceAll( regex: “)\s+”, replacement:”“)
.trim();
∶扫描目录结束,一共得到10460个文件。∶构建文档完毕,一共10460篇文档
扫描目录结束,一共得到10460个文件。∶ 构建文档完毕,一共 10460 篇文档
没有parallel()的情况
最终执行的SQL,使用类似的代码拼接出来String sql=”
insert into forward_indexes (title, url, content) values
list)do
for(Documen
Sql+=",
"(doc.title, doc.url, doc.content)Sql +=’
keyColum
Edit:De PIPOP
title
content url HULL
private Integer
docId;
private String title;private String url;private String content; /
int toIndex)
Returns a view of the portion of this list between the specified fromIndex,inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list. 比如list中有103个元素,一批处理10个from = 0, to = 10 from = 10, to = 20
from = 90, to = 100 from = 100, to = 103
返回原有list的【from,to)的子list出来