java - Most efficient way to count rows of a query -
I am using hibernate to retrieve the number of rows for a specific query. Suppose I have a table called 'person' with different columns. One of those columns is 'name'.
If I want to get the number of people named 'Andrew', which of these methods would be the most efficient? Assuming there is a performance difference between some of them / all Is this a better way to use Hibernate / SQL?
(1) Select all the columns
query query = session.createQuery ("from person where name =: name"); Query.setParameter ("name", name); List results = query.list (); Int count = result.size ();
(2) Just select a column named
query query = session .createQuery ("name of person named name where name =: name") ; Query.setParameter ("name", name); List results = query.list (); Int count = result.size ();
(3) Use of calculation in query
query query = session .createQuery ("Select number from person (*) where name =: Name" ); Query.setParameter ("name", name); Long calculation = (long) query.uniqueResult ();
(4) Use the count with name column in the query
query query = session .createQuery ("Choose name from person (count (name) = : Name "); Query.setParameter ("name", name); Long calculation = (long) query.uniqueResult ();
Edit: Sorry, my list has two numbers 3
If you just want to count the number of rows, do not get results as a result, it means only useless overhead:
- You really want (no matter you
- You will need to send them to the wires
- You will need to create an example (whether it is the full
person
the other Words In, if you want to count only, do not do it on the Java side; DBMS is optimized for this task and will do a better job.
It does not include (1) and (2). < About / P>
(3) and (4), notice that there is a difference between
count (*)
andcount (color)
in general:-
Count (*)
is calculated by all rows
col
-
- They have different results in display and query results. If
col
can be null (count (*)
fast), otherwise similar performance.I (3) use.
same question
Comments
Post a Comment