Skip to content
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

Support descending index #1631

Closed
1 task done
cp-i-pc opened this issue Jun 9, 2021 · 2 comments · Fixed by #2304
Closed
1 task done

Support descending index #1631

cp-i-pc opened this issue Jun 9, 2021 · 2 comments · Fixed by #2304

Comments

@cp-i-pc
Copy link

cp-i-pc commented Jun 9, 2021

CREATE TABLE t (
  c1 INT, c2 INT,
  INDEX idx1 (c1 ASC, c2 ASC),
  INDEX idx2 (c1 ASC, c2 DESC),
  INDEX idx3 (c1 DESC, c2 ASC),
  INDEX idx4 (c1 DESC, c2 DESC)
);
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

func (MyTable) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("c1 DESC", "c2 ASC"),
	}
}

OR

func (MyTable) Indexes() []ent.Index {
        directionalIndex1 := map[string]string{ "c1": "DESC", "c2", "ASC" }
	return []ent.Index{
		index.DirectionalFields(directionalIndex1),
	}
}

Motivation 🔦

I have a table which ID type is not int, but UUID. I'll use ORDER BY time DESC to retrieve latest N records. It says my sorting is Backward index scan when I try to optimize my query using EXPLAIN statement. After doing a little research I realized that the index which created by ent is an ascending index, but what I really needed is descending index. It'll be slower than descending index approximately 15%.

@cp-i-pc cp-i-pc closed this as completed Jun 9, 2021
@cp-i-pc cp-i-pc reopened this Jun 9, 2021
@a8m
Copy link
Member

a8m commented Jun 9, 2021

Hey @ColetteContreras and thanks for proposing this.
We should add support for descending indexes. I just added an annotation option for indexes (in #1632), and this should make it simple to support this option.

cc @rotemtam

@a8m
Copy link
Member

a8m commented Jan 31, 2022

Thanks for proposing this feature @ColetteContreras, it was resolved with #2304.

You can define descending indexes if you're using the Atlas migration. The API is as follows:

index.Fields("a").
	Annotation(entsql.Desc())
// CREATE INDEX ... ON "table" ("a" DESC)

index.Fields("a", "b", "c").
	Annotation(entsql.DescColumns("a", "b"))
// CREATE INDEX ... ON "table" ("a" DESC, "b" DESC, "c")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants