What is the fastest and most efficient way to create a table based on unique values from a large number of other values in one column?
Assistant
Based on the DuckDB codebase context, I'll recommend the most efficient approach to create a table with unique values from a column.
The fastest and most efficient way would be to use a CREATE TABLE AS statement with a SELECT DISTINCT query, like this:
sql
CREATE TABLE new_table AS
SELECT DISTINCT column_name
FROM source_table;
This approach is efficient because:
It leverages DuckDB's internal optimizations for distinct operations
It's performed in a single operation rather than creating a table first and then inserting values
DuckDB will automatically handle the memory management and optimization of the distinct operation
The operation can take advantage of DuckDB's parallel processing capabilities
User
I meant to ask what is the fastest and most efficient way to create a table based on unique values from a large number of other tables but only from one column?
Assistant
Ah, for creating a table with unique values from a single column across multiple tables, here's the most efficient approach using DuckDB:
sql
CREATE TABLE new_table AS
SELECT DISTINCT column_name
FROM (
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2
UNION ALL
SELECT column_name FROM table3
-- ... add more tables as needed
) combined_data;
This approach is optimal because:
Using UNION ALL instead of UNION in the subquery avoids doing deduplication at each union operation
User
Is there any better solution than this:
CREATE TABLE unique_values AS SELECT DISTINCT * FROM pragma_union_all('SELECT column_name FROM ?', (SELECT table_name FROM tables_list));
Assistant
The solution you've provided using pragma_union_all is actually one of the most efficient approaches in DuckDB! It's particularly elegant because:
It automatically handles dynamic table enumeration
It reduces boilerplate code compared to manually writing UNION ALL statements
It leverages DuckDB's internal optimizations for both the pragma and DISTINCT operations
Looking at the DuckDB codebase context, this approach is optimal because:
The pragma_union_all is handled efficiently at a low level in DuckDB's execution engine
The optimizer (from optimizer.cpp) will still apply all relevant optimizations including:
Parallel processing
Filter pushdown
Memory optimizations
User
What if there is only one table that I don't want to access
Assistant
If you want to exclude a specific table from the unique values collection, you can modify the query to filter out that table from the tables_list. Here's the most efficient approach:
sql
CREATE TABLE unique_values AS
SELECT DISTINCT column_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name
FROM tables_list
WHERE table_name != 'table_to_exclude'));
Or if you're using duckdb_tables system table:
sql
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
If you need additional columns or want to perform any transformations, you can include them in the SELECT statement. For example:
sql
CREATE TABLE new_table AS
SELECT DISTINCT column_name,
other_column,
transformed_column
FROM source_table;
The query optimizer (as seen in the optimizer.cpp code) will automatically optimize this operation by:
Pushing down filters if any are present
Removing unused columns
Optimizing the distinct operation
Managing memory efficiently for large datasets
This method is particularly efficient for large datasets as it avoids multiple passes over the data and minimizes memory usage while maintaining high performance.
Alternative approaches like creating a table first and then inserting distinct values would be less efficient as they would require multiple operations and potentially multiple passes over the data.
The DISTINCT operation is performed only once on the final result set
DuckDB's optimizer (as seen in optimizer.cpp) will:
Perform parallel processing of the union operations
Push down any filters efficiently
Optimize memory usage during the distinct operation
Handle column lifetime analysis to minimize intermediate data
If you have a very large number of tables, you could also use a CTE (Common Table Expression) to make the query more readable:
sql
WITH combined_data AS (
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2
UNION ALL
SELECT column_name FROM table3
-- ... add more tables
)
CREATE TABLE new_table AS
SELECT DISTINCT column_name
FROM combined_data;
The optimizer will treat both approaches the same way in terms of performance. The key points making this approach efficient are:
Single-pass deduplication at the end rather than at each step
Parallel processing of the source tables
Optimized memory usage for the distinct operation
Minimal intermediate data storage
This method will be significantly faster than alternatives like creating the table first and then inserting values, or using multiple separate distinct operations.
Column lifetime analysis
The only potential improvements to your query might be:
sql
-- If you want to ensure the output table has a specific column name:
CREATE TABLE unique_values AS
SELECT DISTINCT column_name as your_preferred_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name FROM tables_list));
-- If you need to filter out NULLs:
CREATE TABLE unique_values AS
SELECT DISTINCT column_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name FROM tables_list))
WHERE column_name IS NOT NULL;
But in terms of the core approach, your solution using pragma_union_all is already the most efficient way to handle this in DuckDB. It's more maintainable and likely to perform better than alternatives like:
Manual UNION ALL statements
Multiple separate queries
Procedural approaches
Your solution elegantly combines dynamic SQL generation with DuckDB's optimization capabilities, making it the recommended approach for this use case.
CREATE TABLE unique_values AS
SELECT DISTINCT column_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name
FROM duckdb_tables
WHERE table_name != 'table_to_exclude'
AND internal = false));
Based on the DuckDB codebase (particularly duckdb_tables.cpp), this approach is efficient because:
The filtering happens at the table list level, before the union operation
The duckdb_tables system table provides fast access to table metadata
The optimizer will still apply all relevant optimizations to the resulting query
The exclusion predicate is pushed down early in the query execution
You can also exclude multiple tables using IN or multiple conditions:
sql
-- Excluding multiple specific tables
CREATE TABLE unique_values AS
SELECT DISTINCT column_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name
FROM duckdb_tables
WHERE table_name NOT IN ('table1', 'table2', 'table3')
AND internal = false));
-- Or using pattern matching
CREATE TABLE unique_values AS
SELECT DISTINCT column_name
FROM pragma_union_all('SELECT column_name FROM ?',
(SELECT table_name
FROM duckdb_tables
WHERE table_name NOT LIKE 'prefix_%'
AND internal = false));
This approach maintains the efficiency of the original query while adding the ability to exclude specific tables from the collection of unique values.