简体   繁体   English

使用SQL联接和分组方式时如何计算行数

[英]How to count the number of rows when using SQL joins and group by

I have the following query: 我有以下查询:

SELECT a.HotelID,a.Hotelname,GROUP_CONCAT(DISTINCT b.OperatorName) AS Operators
FROM hotels AS a
INNER JOIN operators AS b
ON a.HotelID = b.HotelID
GROUP BY a.HotelID
ORDER BY a.HotelID
LIMIT 100

I need this query for a simple search function. 我需要此查询以提供简单的搜索功能。 The result Table should contain Paging. 结果表应包含分页。 So what I did was I runned this query (without LIMIT) to get the number of rows (which I need to calculate the pages and so on) and then I rerun that query with the LIMIT. 因此,我要做的是运行此查询(无LIMIT)以获取行数(我需要计算页面数等等),然后使用LIMIT重新运行该查询。

In fact the query itself takes 4-5sec (against 300k table, with indexes on all the fields) which means it currently takes 10sec to load because it runs two times. 实际上,查询本身需要4到5秒的时间(相对于300k表,所有字段都有索引),这意味着它当前需要10秒才能加载,因为它运行了两次。

I am wondering if there is a SQL Statement I can simply use to get the number of rows and which might be faster. 我想知道是否有一条SQL语句可以简单地用于获取行数,而这可能会更快。 I thought I can use COUNT(a.HotelID) but this not works. 我以为我可以使用COUNT(a.HotelID),但这不起作用。

update 更新

select count(*) from (
SELECT distinct b.HotelID
FROM hotels AS a
INNER JOIN operators AS b
ON a.HotelID = b.HotelID    
)

can this be faster? 可以更快吗?

give this a try: 试试看:

SELECT *
FROM (
    SELECT a.HotelID,a.Hotelname,GROUP_CONCAT(DISTINCT b.OperatorName) AS Operators, COUNT(a.HotelID) AS total
    FROM hotels AS a
    INNER JOIN operators AS b
    ON a.HotelID = b.HotelID
    GROUP BY a.HotelID
    ) AS a
ORDER BY a.HotelID
LIMIT 100

also, for the speed you should make sure your indexes are in order. 同样,为了提高速度,您应该确保索引正确。

Clearly described in the manual : 手册中有明确说明

SQL_CALC_FOUND_ROWS tells MySQL to calculate how many rows there would be in the result set, disregarding any LIMIT clause. SQL_CALC_FOUND_ROWS告诉MySQL忽略任何LIMIT子句,计算结果集中将有多少行。 The number of rows can then be retrieved with SELECT FOUND_ROWS() . 然后可以使用SELECT FOUND_ROWS()检索行数。 See Section 11.13, “Information Functions”. 请参见第11.13节“信息功能”。

If you follow the link to Section 11.13, there's then an example: 如果您单击第11.13节的链接,则有一个示例:

FOUND_ROWS()

A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. SELECT语句可以包含LIMIT子句,以限制服务器返回给客户端的行数。 In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT , but without running the statement again. 在某些情况下,希望知道在没有LIMIT情况下该语句将返回多少行,而无需再次运行该语句。 To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward: 要获得此行计数,请在SELECT语句中包括一个SQL_CALC_FOUND_ROWS选项,然后再调用FOUND_ROWS()

 mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name -> WHERE id > 100 LIMIT 10; mysql> SELECT FOUND_ROWS(); 

The second SELECT returns a number indicating how many rows the first SELECT would have returned had it been written without the LIMIT clause. 第二个SELECT返回一个数字,该数字指示如果没有LIMIT子句而写入的第一个SELECT将返回多少行。

In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement. 如果在最近成功执行的SELECT语句中没有SQL_CALC_FOUND_ROWS选项,则FOUND_ROWS()返回该语句返回的结果集中的行数。 If the statement includes a LIMIT clause, FOUND_ROWS() returns the number of rows up to the limit. 如果该语句包含LIMIT子句,则FOUND_ROWS()返回不超过限制的行数。 For example, FOUND_ROWS() returns 10 or 60 , respectively, if the statement includes LIMIT 10 or LIMIT 50, 10 . 例如,如果语句包含LIMIT 10LIMIT 50, 10 FOUND_ROWS() ,则FOUND_ROWS()返回1060

Please, use the documentation as your first port of call. 请使用文档作为您的第一个停靠港。

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

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