How to reuse a large query without repeating it?

后端 未结 5 1845
别那么骄傲
别那么骄傲 2020-12-31 06:58

If I have two queries, which I will call horrible_query_1 and ugly_query_2, and I want to perform the following two minus operations on them:

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 07:29

    If you want to reuse the SQL text of the queries, then defining views is the best way, as described earlier.

    If you want to reuse the result of the queries, then you should consider global temporary tables. These temporary tables store data for the duration of session or transaction (whichever you choose). These are really useful in case you need to reuse calculated data many times over, especially if your queries are indeed "ugly" and "horrible" (meaning long running). See Temporary tables for more information.

    If you need to keep the data longer than a session, you can consider materialized views.

提交回复
热议问题