Fetching rows from multiple tables with UNION ALL or using one table in production?

人走茶凉 提交于 2019-12-11 03:06:25

问题


I know that for relational database like Postgresql using separated tables would be more efficient but I'm concerning for performance issues because the most executed query will fetch rows from multiple tables using UNION ALL.

I have to option to handle this problem. First one is:

table1 -> column1, column2
table2 -> column1, column2
table3 -> column1, column2, column3

In this solution I have to use 3 different query merged with UNION ALL in production and this query will be performed a user logged in the system (the most executed query in the system)

The other is:

table -> column1, column2, typeColumn, extraColumnForTable3

In this solution I have to create an extra column typeColumn to distinguish which type the row is. And I also have to create a column extraColumnForTable3 for the type table3 and it will be NULL for table2 and table1 type. In this solution the most executed query will include only one SELECT statement.

There will be million of rows in production so I'm concerning about performance. NULL values may occupy an extra space in database but I think it can be negligible. I will use partial index that eliminates NULL values so I don't think it will affect the other queries that fetch specific types. Which one do you think more efficient in production?


回答1:


In general I find that extensive use of UNION suggests bad database design. There are cases where UNION and UNION ALL make sense but they should be relatively rare outside of recursive common table expressions.

PostgreSQL provides a fairly large number of options for keeping performance on a single table manageable, and as you point out partial indexes are a very good way to manage this problem.

The major problem with breaking up tables such that such UNION statements are common is that it makes primary and foreign key management quite problematic. In general it is almost always far better to make sure your data structure is clear and manageable first, and then worry about optimization than it is to worry about optimization and then try to make the optimized solution manageable.



来源:https://stackoverflow.com/questions/14833577/fetching-rows-from-multiple-tables-with-union-all-or-using-one-table-in-producti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!