gpt4 book ai didi

mysql - 提高 Solr 和 MySQL 对的性能(使用 JPA 通过 "WHERE IN"连接)

转载 作者:行者123 更新时间:2023-11-29 01:04:02 27 4
gpt4 key购买 nike

我有一个由 Solr 索引的 MySQL 数据库。我使用 Solr(快速)执行搜索,并使用 JPA 从数据库中检索 Solr 搜索中的每个结果。 JPA 在数据库上运行一个非常慢的 WHERE IN 查询。

有没有办法加快这个过程,或者重构设计来提高性能?

我刚刚将整个应用程序从使用 MySQL 的全文搜索重构为使用 Solr,现在性能更差了。

注意:我需要立即对所有结果进行计算,因此无法使用分页。

Java代码:

    SolrDocumentList documentList = response.getResults();
Collection<String> listingIds = new ArrayList<>();
for(SolrDocument doc : documentList) {
String listingId = (String) doc.getFirstValue("ListingId");
listingIds.add(listingId);
}

Query query = em.createNamedQuery("getAllListingsWithId");
query.setParameter("listingIds", listingIds);
List<ListedItemDetail> listings = query.getResultList();

命名查询:

<query>Select listing from ListingSet listing where listing.listingId in :listingIds</query>

附加信息:

SHOW CREATE TABLE ListingSet 生成 [缩短]:

CREATE TABLE `listingset` (
`LISTINGID` int(11) NOT NULL,
`STARTDATE` datetime DEFAULT NULL,
`STARTPRICE` decimal(10,2) DEFAULT NULL,
`TITLE` varchar(255) DEFAULT NULL,
PRIMARY KEY (`LISTINGID`),
KEY `FK_LISTINGSET_MEMBER_MEMBERID` (`MEMBER_MEMBERID`),
CONSTRAINT `FK_LISTINGSET_MEMBER_MEMBERID` FOREIGN KEY (`MEMBER_MEMBERID`) REFERENCES `member` (`MEMBERID`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1

调查生成的 SQL

查看生成的 SQL,JPA 为单个 JPA 查询运行大量 SQL 查询。 ListingSet 表有 7 个链接到的表,并为每个表运行单独的 SELECT 查询以获取 EACH listingid(其中有 1,000 - 10,000)。所以我的一个 JPA 查询变成了大约 7,000 个查询!

最佳答案

以下是调试问题的个人想法:

  • 开启mysql查询日志并检查 JPA 是否在每个 listingId 的每个查询中都访问 MySQL。

    mysql -uroot -pYOUR-PASSWORD -e "SET GLOBAL log_output = 'FILE'; 设置 GLOBAL general_log_file = '/tmp/mysql.log'; SET GLOBAL general_log = 'ON';"tail -f/tmp/mysql.log

  • 检查性能是否由 MySQL 引起,在您的 MySQL 数据库中运行等效的 SQL。

    从ListingSet where listingId中选择listing(把你的真实listingId放在这里);

    确保在 ListingId 列上有索引(很有可能索引已经存在)

  • 因为你只从 MySQL 中读取行,也许你可以为更多的从属设置复制,然后将您的 ListingIds 拆分到所有从属 MySQL,然后合并结果。 http://dev.mysql.com/doc/refman/5.0/en/replication-howto.html

关于mysql - 提高 Solr 和 MySQL 对的性能(使用 JPA 通过 "WHERE IN"连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com