tursodatabase / libsql

libSQL is a fork of SQLite that is both Open Source, and Open Contributions.

Home Page:https://turso.tech/libsql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SELECT Query still does a FULL TABLE SCAN.

3g015st opened this issue · comments

commented

I'm not quite sure on why Turso still does a full table scan on my table. Here's the timeline of events.

1.) Created Users table, with columns userId, socialId and userType. Only index here is the userId column as the primary key.
2.) Table is now live at this point with user rows being created.
3.) Made a query to fetch users based on socialId and userType.
4.) Noticed on the Turso Analytics that the query is fetching 100 rows per execution, even though it's just returning a single row.
5.) Made a compound index for socialId and userType
6.) Observed the same query, now with the index in place but the results are still the same.
7.) Made a database branch for that database and this time I deleted all the rows for the Users table. Because I thought maybe the index won't work because of the existing rows.
8.) Observed the query again for the new database with deleted rows, same results.

I'm lost at this point and I'm thinking of moving forward with another provider. I am using Turso with prisma, but for the query itself I am using a raw sql query.

Please share the schema and queries you're using @3g015st

commented

@haaawk Sorry for the late reply, here it is:
Query:

  SELECT id, imageUrl, socialId, userType 
  FROM Users
  WHERE
  (
    userType = "SUBSCRIBED_PLAYER"
    socialId = <v4-uuid>
    AND DATETIME(scheduledBoostAt) >= CURRENT_TIMESTAMP

Schema

model Users {
  id              String            @id @default(uuid())
  socialId String @unique
  email           String            @unique
  scheduledBoostAt  DateTime?
  createdAt       DateTime          @default(now())
  updatedAt       DateTime?
  userType          String
  referralCode    String            @unique
  Items        Items[]
}

Query itself returns a single row on my app server, but when executed it's like 100 row reads on Turso analytics. Weird enough because I have 100-ish rows on that table, so maybe it is doing a FULL TABLE SCAN. But when I used the EXPLAIN QUERY PLAN it is using the index that I made.

is the issue still not assigned ??