3 ms·
1. Picking a random sub-range is not the same thing as random sampling. See other comments here and all over the Internet. 2. To truly random sample k out of a
by alecco 6y ago
1. Picking a random sub-range is not the same thing as random sampling. See other comments here and all over the Internet.
2. To truly random sample k out of a set of n rows it's needed to know the set first.
3. Walking the rowid B-Tree should be relatively fast (and necessary because of 2.)
It may be the case a sub-range is good enough (e.g. the table is quite disordered relatively to the properties looked after). But this is very unusual and you should warn the users of this data because later on they might change
Imagine your sub-range picks rows created over the weekend or some other particular time-frame. Or an import from some other legacy system. This will not be representative of the full set in many ways.
If the system needs to do a lot of sampling queries, perhaps it would be better to make overnight an auxiliary table. You can make many sample tables and compare which ones deviate less from the actual table. This table will be small and could even have materialized views pre-computed with the most expensive computations.