获取表行计数的最有效方法

[英]Most efficient way to get table row count


I currently have a database with over 6 million rows and growing. I currently do SELECT COUNT(id) FROM table; in order to display the number to my users, but the database is getting large and I have no need to store all of those rows except to be able to show the number. Is there a way to select the auto_increment value to display so that I can clear out most of the rows in the database? Using LAST_INSERT_ID() doesn't seem to work.

我现在有一个超过600万行的数据库,并且还在增长。我目前从表中选择COUNT(id);为了向我的用户显示数字,但是数据库越来越大,我不需要存储所有这些行,除了能够显示数字。是否有一种方法可以选择要显示的auto_increment值,这样我就可以清除数据库中的大部分行?使用LAST_INSERT_ID()似乎不起作用。

12 个解决方案

#1


59  

If it's only about getting the number of records (rows) I'd suggest using:

如果只是关于获取记录(行)的数量,我建议使用:

SELECT TABLE_ROWS
FROM information_schema.tables 
WHERE table_name='the_table_you_want' -- Can end here if only 1 DB 
  AND table_schema = DATABASE();      -- See comment below if > 1 DB

(at least for MySQL) instead.

(至少对MySQL来说)。

#2


87  

Following is the most performant way to find the next AUTO_INCREMENT value for a table. This is quick even on databases housing millions of tables, because it does not require querying the potentially large information_schema database.

下面是找到表的下一个AUTO_INCREMENT值的最有效方法。即使在包含数百万个表的数据库上,这也是非常快速的,因为它不需要查询潜在的大型information_schema数据库。

mysql> SHOW TABLE STATUS LIKE 'table_name';
// Look for the Auto_increment column

However, if you must retrieve this value in a query, then to the information_schema database you must go.

但是,如果您必须在查询中检索此值,则必须转到information_schema数据库。

SELECT `AUTO_INCREMENT`
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_SCHEMA = 'DatabaseName'
AND    TABLE_NAME   = 'TableName';

#3


29  

try this

试试这个

Execute this SQL:

执行此SQL:

SHOW TABLE STATUS LIKE '<tablename>'

and fetch the value of the field Auto_increment

并获取字段Auto_increment的值。

#4


16  

I'm not sure why no one has suggested the following. This will get the auto_increment value using just SQL (no need for using PHP's mysql_fetch_array):

我不知道为什么没有人提出以下建议。这将使用SQL获取auto_increment值(不需要使用PHP的mysql_fetch_array):

SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_NAME = 'table'

#5


2  

if you directly get get max number by writing select query then there may chance that your query will give wrong value. e.g. if your table has 5 records so your increment id will be 6 and if I delete record no 5 the your table has 4 records with max id is 4 in this case you will get 5 as next increment id. insted to that you can get info from mysql defination itself. by writing following code in php

如果您通过编写select查询直接获得最大编号,那么您的查询可能会给出错误的值。例如,如果您的表有5条记录所以你的增量id将6,如果我删除记录没有5你表4记录与马克斯id是在这种情况下,你会得到5下一个增量id.本月从mysql的观念,你可以信息本身。通过使用php编写以下代码

<?
$tablename      = "tablename";
$next_increment     = 0;
$qShowStatus        = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult  = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus );

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

#6


1  

SELECT id FROM table ORDER BY id DESC LIMIT 1 can returns max id not auto increment id. both are different in some conditions

根据id DESC LIMIT 1从表顺序中选择id可以返回最大id而不是自动增量id

#7


1  

If you do not have privilege for "Show Status" then, The best option is to, create two triggers and a new table which keeps the row count of your billion records table.

如果您没有“显示状态”的特权,那么最好的选择是创建两个触发器和一个新表,该表保存您的十亿记录表的行数。

Example:

例子:

TableA >> Billion Records
TableB >> 1 Column and 1 Row

表a >> >的记录表>> 1列和1行。

Whenever there is insert query on TableA(InsertTrigger), Increment the row value by 1 TableB
Whenever there is delete query on TableA(DeleteTrigger), Decrement the row value by 1 in TableB

在表a (InsertTrigger)上有插入查询时,在表a (DeleteTrigger)上有删除查询时,将行值增加1个表b,在表b中减少1个行值

#8


0  

Next to the information_schema suggestion, this:

在information_schema建议下,以下是:

SELECT id FROM table ORDER BY id DESC LIMIT 1

should also be very fast, provided there's an index on the id field (which I believe must be the case with auto_increment)

如果id字段上有索引(我认为auto_increment也是这样),那么应该也会非常快

#9


0  

$next_id = mysql_fetch_assoc(mysql_query("SELECT MAX(id) FROM table"));
$next_id['MAX(id)']; // next auto incr id

hope it helpful :)

希望它有帮助:)

#10


0  

Controller

控制器

SomeNameModel::_getNextID($this->$table)

MODEL

模型

class SomeNameModel extends CI_Model{

private static $db;

function __construct(){
  parent::__construct();
  self::$db-> &get_instance()->db;
}


function _getNextID($table){
  return self::$db->query("SHOW TABLE STATUS LIKE '".$table."' ")->row()->Auto_increment;
}

... other stuff code

}

#11


0  

None of these answers seem to be quite right. I tried them all. Here are my results.

这些答案似乎都不十分正确。我试着。这是我的结果。

Sending query: SELECT count(*) FROM daximation
91
Sending query: SELECT Auto_increment FROM information_schema.tables WHERE table_name='daximation'
96
Sending query: SHOW TABLE STATUS LIKE 'daximation'
98
Sending query: SELECT id FROM daximation ORDER BY id DESC LIMIT 1
97

here's the screenshot: https://www.screencast.com/t/s8c3trYU

这是截图:https://www.screencast.com/t/s8c3trYU

Here is my PHP code:

这是我的PHP代码:

$query = "SELECT count(*) FROM daximation"; 
$result = sendquery($query);
$row = mysqli_fetch_row($result);
debugprint( $row[0]);

$query = "SELECT Auto_increment FROM information_schema.tables WHERE table_name='daximation'"; 
$result = sendquery($query);
$row = mysqli_fetch_row($result);
debugprint( $row[0]);

$query = "SHOW TABLE STATUS LIKE 'daximation'"; 
$result = sendquery($query);
$row = mysqli_fetch_row($result);
debugprint( $row[10]);

$query = "SELECT id FROM daximation ORDER BY id DESC LIMIT 1"; 
$result = sendquery($query);
$row = mysqli_fetch_row($result);
debugprint( $row[0]);

#12


-1  

Couldn't you just create a record in a separate table or whatever with a column called Users and UPDATE it with the last inserted id on User Registration?

难道你不能在一个单独的表中创建一个记录或者用一个叫做Users的列来更新它并在用户注册时使用最后插入的id吗?

Then you would just check this field with a simple query.

然后您只需使用一个简单的查询来检查这个字段。

It might be rough but it would work perfectly.

它可能很粗糙,但会完美地工作。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/06/01/d250ef511ca18180f8ec25f03357cdc0.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号