简体   繁体   English

计算查询行的最有效方法

[英]Most efficient way to count rows of a query

I'm using Hibernate to retrieve the number of rows for a specific query. 我正在使用Hibernate来检索特定查询的行数。 Let's say I have a table called 'Person' with various columns. 假设我有一个名为'Person'的表,其中包含各种列。 One of those columns is 'name'. 其中一列是'name'。

If I wanted to get the number of people with the name of 'Andrew', which of these ways would be most efficient? 如果我想获得名为“安德鲁”的人数,哪种方式最有效? Assuming there is a performance difference between some/all of them. 假设它们中的一些/全部存在性能差异。 Is there a better way to do this using Hibernate/SQL? 有没有更好的方法来使用Hibernate / SQL?

(1) Select all columns (1)选择所有列

Query query = session.createQuery("from Person where name= :name");
query.setParameter("name", name);
List result = query.list();
int count = result.size();

(2) Select just the name column (2)只选择名称列

Query query = session.createQuery("select name from Person where name= :name");
query.setParameter("name", name);
List result = query.list();
int count = result.size();

(3) Using Count in the query (3)在查询中使用Count

Query query = session.createQuery("select count(*) from Person where name= :name");
query.setParameter("name", name);
long count = (Long) query.uniqueResult();

(4) Using Count with the name column in the query (4)在查询中使用Count和name列

Query query = session.createQuery("select count(name) from Person where name= :name");
query.setParameter("name", name);
long count = (Long) query.uniqueResult();

Edit: Sorry, I had two number 3's in my list 编辑:对不起,我的列表中有两个3号

Don't retrieve a result set if you just want to count the number of rows, this just means useless overhead: 如果您只想计算行数,请不要检索结果集,这只是意味着无用的开销:

  • you'll get more stuff than actually wanted (whether you're selecting all columns or just one) 你会获得比实际想要更多的东西(无论你是选择所有列还是只选择一个)
  • you'll need to send them over the wire 你需要通过电线发送它们
  • you'll need to create instances (whether it's a full Person entity or just a String ) for nothing. 你需要创建实例(无论是完整的Person实体还是只是一个String )。

In other words, if you only want to count, don't do it on the Java side, DBMS are optimized for this task and will do a much better job. 换句话说,如果您只想计算,不要在Java端执行,DBMS针对此任务进行了优化,并且可以做得更好。

This excludes (1) and (2). 这不包括(1)和(2)。

Regarding (3) and (4), note that there is a difference between count(*) and count(col) in general: 关于(3)和(4),请注意一般count(*)count(col)之间存在差异:

  • count(*) counts ALL rows count(*)计算所有
  • count(col) counts rows with non-null values of col count(col)计算与非空值的行col

So they will give different results in performance and query result if col can be NULL (the count(*) being faster), otherwise identical performance. 因此,如果col可以为NULL( count(*)更快),则它们将在性能和查询结果中给出不同的结果,否则性能相同。

I'd use (3). 我用(3)。

Similar questions 类似的问题

The count(*) method has profiled to be significantly faster than the size() method for my company. count(*)方法的分析速度明显快于我公司的size()方法。 It is certainly more memory efficient since you aren't pulling across column data that you won't use. 它肯定更节省内存,因为您没有跨越您不会使用的列数据。 I don't know if count(name) makes a difference. 我不知道count(name)是否count(name)

The less you put inside the COUNT() function the better. 你放入COUNT()函数越少越好。 If you don't need any of the information from the table, I would say use COUNT(1) . 如果您不需要表中的任何信息,我会说使用COUNT(1) You can use COUNT(name) or COUNT(*) as long as your tables are properly indexed. 只要表格被正确编入索引,就可以使用COUNT(name)COUNT(*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM