-
Notifications
You must be signed in to change notification settings - Fork 137
How to store intermediate computation results #871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello !
Temporary tables are really designed for this kind of things, do you have something in particular in your setup preventing you from being able to use them ? |
Actually, I'm not sure how to manage temporary tables in my context. We use sqlpage for handling HTTPD connections and LDAP authentication with the function sqlpage.basic_auth_username(). But I’m wondering how temporary tables behave in this context:
Let’s say I do this: SET run_sql_result = sqlpage.run_sql('fetch_schedule.sql'); And fetch_schedule.sql contains: DROP TABLE IF EXISTS temp_schedule_table;
CREATE TEMP TABLE temp_schedule_table AS
SELECT * FROM schedule; Then I can call this table like this: FROM temp_schedule_table; But how will this behave for each user? Will each user have their own instance of temp_schedule_table? When will this table be dropped, especially in a multi-user environment? I’m trying to better understand this mechanism in the context of using authentication and session management via LDAP. CTEs are not a solution in my case, as they would need to be repeated for each query on the same page. |
Thank you for the clarification ! These are good questions. Temporary tables and database connectionsTemporary tables are tied to the lifetime of network connections to the database:
How SQLPage handles the database connection lifetimeSQLPage gives the following guarantees for database connections:
In conclusionYour code seems to be correct. -- In case the connection is not fresh, we may have a temp table from a previous request. Just drop it
DROP TABLE IF EXISTS temp_schedule_table;
-- Create a new temporary table in order to store the results of a heavy computation.
-- We are guaranteed to be able to access this table from all other sql
-- queries that will run during the rendering of the current web page.
CREATE TEMP TABLE temp_schedule_table AS SELECT * FROM schedule where some_condition; |
Hi @theochr72 You use PostgreSQL as database, you can also use Materialized Views
|
Thank you for all your advice, I have chosen the temporary table. It looks good, I will see depending on the number of users if it impacts the database performance. Since I'm not exactly sure how the connections will be managed with httpd and basic auth for each user, as well as the lifespan of the temporary tables and their behavior in my context, it seems perfect for my use case. @kryskool, thanks for your suggestion, but these will be quite similar tables, and I find it less clean in my case than using temporary tables. Thanks again! |
Hello,
I’m working on an SQL page that makes several calls to the same table, with about ten columns, joins, and filters. This table is quite large, and I’m wondering if it’s possible to store this SELECT in a variable or call it recursively. Additionally, I’d like to be able to apply further filters afterwards. I want to avoid creating a temporary table. I’m working with a PostgreSQL database. If you have any ideas, I’d be happy to hear them!
The text was updated successfully, but these errors were encountered: