【问题标题】:How to fetch next records who have same app_id column but different mobile number column如何获取具有相同app_id列但不同手机号码列的下一条记录
【发布时间】:2021-08-25 06:10:41
【问题描述】:

我在 MySQL 员工表中有 4 亿条数据我想每次获取 100 万条记录,因为我在获取前 100 万条记录后使用了限制我想获取接下来的 100 万条记录 记录。

查询==>

select mobile_number ,app_id from employee where department_id='comp' and app_id between '150005050000' and 150005058888' order by app_id limit 0, 100000;

 mobile_number      app_id
| 919xxxxxxx54  |    150005050035 |   <-----  first 1 million nth record
| 9199xxxxxxx6  |    150005050035 |   <------ I want fetch next 1 million records from this line
| 919xxxxxxx35  |    150005050035 |
| 9189xxxxxxx1  |    150005050036 |
| 9199xxxxxxx6  |    150005050036 | 
+---------------+-----------------+

假设这是结果,我得到了我的前 100 万条记录,我想获取下 100 万条记录,但问题是在我的表中 app_id 列有重复值,所以我将如何获取下一条记录 mobile_number 和 app_id 列的组合将使该行是唯一的,所以有什么解决方案,所以我将获取接下来的 100 万条记录,如上表所示;

【问题讨论】:

  • 如何编写java jdbc应用程序来获取前100万条记录之后的记录

标签: java mysql query-optimization


【解决方案1】:

要获取下一个 100 万条记录,只需将您的限制中的 0 更改为 1000000,无论是否具有相同的 id。

select mobile_number ,app_id from employee where department_id='comp' and app_id between '150005050000' and 150005058888' order by app_id limit 1000000, 1000000;

这将跳过前 100 万个并获取下一个 100 万个。

要获取第三个 100 万,只需在您的限额中再增加 100 万,例如

select mobile_number ,app_id from employee where department_id='comp' and app_id between '150005050000' and 150005058888' order by app_id limit 2000000, 1000000;

等等。

LIMIT中的第一个数字是要跳过的数据,第二个数字是要返回的数据个数。

【讨论】:

  • 但是当我们将限制从 0 更改为 100000 时,如果我们有钱限制 1900000 选择 mobile_number ,app_id from employee where department_id='comp' and app_id between '150005050000' and 150005058888' order by app_id 限制 1900000, 100000;我如何使用 mobile_number 和 app_id 列组合来获取下一条记录是否有任何运算符或任何其他方式,以便我们可以使用在特定的 mobile_number 和 app_id 列之后获取记录
  • 这只是您关于如何获取下一百万数据的问题的答案。为了减少等待时间是另一个需要解决的问题,这将是自然发生的,因为您的数据库中有大量数据。
  • 可以存储最后n个手机和id,使用WHERE子句
  • 如果您有一个auto-increment 列会很有帮助,这样您就可以轻松使用where id&gt;nth,但这是不可能的,因为您已经有一个包含大量数据的表并添加了一个auto-increment列需要很长时间。
  • 您可以read this 获取一些想法。
【解决方案2】:

如果这是您想要使用表格的方式,我同意 ErrBon 的建议 - 在您的表格上放置一个自动递增的 id 列。

【讨论】:

  • 如果我添加了自动增量 id 列,那么它会降低查询的性能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多