gpt4 book ai didi

java - java和mysql中相同SQL查询的运行时间差异巨大

转载 作者:行者123 更新时间:2023-11-29 12:49:45 24 4
gpt4 key购买 nike

我有一个功能如下:

public static ArrayList<Article> getNewsList(Database db, Date start,Date end,int nextNth,int limit,String tableName,String entityQuery,String topicQuery) throws SQLException {
long a=System.currentTimeMillis();
ArrayList<Article> youTube=new ArrayList<Article>();
System.out.println("starting time is : "+a+" ");
Connection conn = db.getConnection();
PreparedStatement stmt = conn
.prepareStatement("SELECT n.title,n.link,n.Description,n.NewsContent,n.IURL,date(n.Published) FROM "+tableName+" as en inner join news as n on "
+ " en.dataitemid=n.id where en.dataitemtype=1 and dateAdded between ? and ? "+entityQuery+" "+topicQuery+" limit ?,?;");
stmt.setDate(1, start);
stmt.setDate(2, end);
stmt.setLong(3, nextNth);
stmt.setLong(4, limit);

ResultSet rset = stmt.executeQuery();
while (rset.next()) {
Article nw=new Article();
nw.setTitle(rset.getString(1));
nw.setLink(rset.getString(2));
nw.setDescription(rset.getString(3));
nw.setContent(rset.getString(4));
nw.setiURL(rset.getString(5));
nw.setDate(rset.getDate(6));

youTube.add(nw);
}
long b=System.currentTimeMillis();
System.out.println(b+" total time it takes is: "+(b-a));
return youTube;
}

这个函数的行为非常有趣。如您所见,我有 2 个变量用于函数的开始和结束时间,当我运行该函数时,需要 36 秒(b-a 的结果)

但是,如果我在 mysql 中使用相同的参数运行相同的查询,则只需要 0.019 秒,这显示了巨大的差异,尽管如您所见,我的 java 代码中没有任何需要很长时间的特殊内容。我很好奇是什么导致了这种巨大的差异。有人可以帮忙吗?

最佳答案

此处可能涉及网络延迟/错误。尝试在执行之前和执行之后移动 a 和 b 时间并查看该值。在您的示例中,您没有提及要检索的行数,但这可能会影响您的计时。

关于java - java和mysql中相同SQL查询的运行时间差异巨大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24942961/

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