如何从一个PostgreSQL表中检索数据的随机行? [英] How to retrieve randomized data rows from a postgreSQL table?

查看:164
本文介绍了如何从一个PostgreSQL表中检索数据的随机行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PostgreSQL服务器数据库表上几乎 3000000 行,我需要的所有行保存到一个CSV文件。这里的问题是,该行必须被保存在一个不同的随机我每次运行过程时间顺序。

I have a table on a PostgreSQL server database with almost 3 million rows and I need to save all rows to a CSV file. The problem here is that the rows must be saved in a different random order each time I run the process.

值得一提的是,这是一个C# 。WinForms应用程序

Worth noting that it's a C# WinForms app.

我试图将数据加载到一个List<>以及使用的洗牌算法,但它采取了永远结束了,我真的需要它要快。我想直接做在查询会快很多,但我并没有对如何做到这一点的线索。

I tried loading the data into a List<> and using a shuffling algorithm but it took forever to finish, and I realy need it to be fast. I think doing it directly in the query will be much faster but I don't have a clue on how to do it.

推荐答案

通常情况下,你可以只 ORDER BY 你的数据库味道随机函数,如

Typically, you can just ORDER BY a random function of your database flavor, like

SELECT * FROM table ORDER BY RANDOM()

这可能是因为洗牌慢列表中,这取决于该数据库服务器或产品上。

This might be as slow as shuffling the list, depending on the database server or product.

这篇关于如何从一个PostgreSQL表中检索数据的随机行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆