-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
opt: derived projections shouldn't be pushed past sorts #32952
Comments
Sounds like we should be taking the number of total columns into account for sort. |
Also, I wondered many times if it would make sense to swap the order in which we evaluate things, specifically to do enforcers last. I believe it might help in practice, even if the right answer is always that if a plan is better than another, the cost should reflect that. |
I think the coster just needs a good overhaul. It's still very simplistic, and so we see these kinds of anomalies. I'm planning to look into it more when I work on incorporating latencies into the cost function. |
Currently, if we have to sort results and project a new column, there is no cost difference between the two orders and we happen to prefer the sort on top. It is preferable to sort before adding new columns to avoid storing the extra value in memory or on disk. This change improves the sort costing by adding a cost proportional to the total number of values. Fixes cockroachdb#32952.
60154: sql: add crdb_internal.show_create_all_tables builtin r=rafiss a=RichardJCai sql: add crdb_internal.show_create_all_tables builtin Making this a PR first before continuing with #53488, we can alias this builtin with SHOW CREATE ALL TABLES. Example output: ``` query T SELECT crdb_internal.show_create_all_tables('d') ---- CREATE TABLE public.parent ( x INT8 NULL, y INT8 NULL, z INT8 NULL, UNIQUE INDEX parent_x_y_z_key (x ASC, y ASC, z ASC), UNIQUE INDEX parent_x_key (x ASC), FAMILY f1 (x, y, z, rowid) ); CREATE TABLE public.full_test ( x INT8 NULL, y INT8 NULL, z INT8 NULL, UNIQUE INDEX full_test_x_key (x ASC), FAMILY f1 (x, y, z, rowid) ); CREATE SEQUENCE public.s MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1; CREATE VIEW public.vx ("?column?") AS SELECT 1; ALTER TABLE public.full_test ADD CONSTRAINT fk_x_ref_parent FOREIGN KEY (x, y, z) REFERENCES public.parent(x, y, z) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE public.full_test ADD CONSTRAINT test_fk FOREIGN KEY (x) REFERENCES public.parent(x) ON DELETE CASCADE; -- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES ALTER TABLE public.full_test VALIDATE CONSTRAINT fk_x_ref_parent; ALTER TABLE public.full_test VALIDATE CONSTRAINT test_fk; ``` Release note (sql change): crdb_internal.show_create_all_tables is a new builtin that takes in a database name (string) and returns a flat log of all the CREATE TABLE statements in the database followed by alter statements to add constraints. The output can be used to recreate a database. This builtin was added to replace old dump logic. 60469: opt: prefer sorting fewer columns r=RaduBerinde a=RaduBerinde Currently, if we have to sort results and project a new column, there is no cost difference between the two orders and we happen to prefer the sort on top. It is preferable to sort before adding new columns to avoid storing the extra value in memory or on disk. This change improves the sort costing by adding a cost proportional to the total number of values. Fixes #32952. Co-authored-by: richardjcai <caioftherichard@gmail.com> Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
Here's the plan for TPCH Q1:
As you can see, the optimizer pushes the "derived" render expressions (projections that aren't part of the scan) past the sort. This is inefficient, as it requires that the sort shuffle more memory.
Ideally, the optimizer should not materialize any extra projections below a sort, to avoid this kind of shuffling.
The text was updated successfully, but these errors were encountered: