prisma: Full-text search query is very slow with `search` API but fast with raw SQL

The Drizzle benchmarks use the search API for full-text search (that’s currently in Preview) in two routes:

The slow performance of the query has a heavy impact on the benchmark results leading to Prisma ORM being vastly slower than Drizzle.

When replacing the search query with raw SQL, the performances of Prisma ORM and Drizzle are on par. Here’s how you can replace the search queries with $queryRaw for better performance:

// /search-customer
const term = `${c.req.query("term")}:*`;
const result = await prisma.$queryRaw`select * from "products" where to_tsvector('english', "products"."name") @@ to_tsquery('english', ${term});`;

// /search-product
const term = `${c.req.query("term")}:*`;
const result = await prisma.$queryRaw`select * from "products" where to_tsvector('english', "products"."name") @@ to_tsquery('english', ${term});`;

The problem may be related with this issue mentioning that indexes are not properly used during FTS: https://github.com/prisma/prisma/issues/8950#issue-980473627

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 34
  • Comments: 18 (2 by maintainers)

Most upvoted comments

Hi, I would also like an update on this. It seems very strange that a problem that has been flagged for what is nearly 3 years based on https://github.com/prisma/prisma/issues/8950#issue-980473627 has not had any really update / fix. @janpio @nikolasburk can you provide any update on when this may potentially be fixed?

Hi!

Is there an update on this? Using raw query is not a great solution, as it goes against the spirit of using the library. Do you have a rough timeline to share? Would appreciate any updates. Thanks!

+1 need this as well

+1

Some initial investigation:

/search-product code:

For Prisma: https://github.com/drizzle-team/drizzle-benchmarks/blob/73107aadb3942489a3d9bd8093d1636fe7f87465/src/prisma-server.ts#L106-L116

app.get("/search-product", async (c) => {
  const result = await prisma.product.findMany({
    where: {
      name: {
        search: `${c.req.query("term")}:*`,
      },
    },
  });

  return c.json(result);
});

For Drizzle: https://github.com/drizzle-team/drizzle-benchmarks/blob/73107aadb3942489a3d9bd8093d1636fe7f87465/src/drizzle-server.ts#L206-L211 https://github.com/drizzle-team/drizzle-benchmarks/blob/73107aadb3942489a3d9bd8093d1636fe7f87465/src/drizzle-server.ts#L90-L96

const p10 = db.query.products
  .findMany({
    where: sql`to_tsvector('english', ${
      products.name
    }) @@ to_tsquery('english', ${placeholder("term")})`,
  })
  .prepare("p10");
  
 ...
 
 app.get("/search-product", async (c) => {
  // const term = `%${c.req.query("term")}%`;
  const term = `${c.req.query("term")}:*`;
  const result = await p10.execute({ term });
  return c.json(result);
});

Generated SQL queries

For Prisma:

Query: 
SELECT "public"."products"."id", "public"."products"."name", "public"."products"."qt_per_unit", "public"."products"."unit_price", "public"."products"."units_in_stock", "public"."products"."units_on_order", "public"."products"."reorder_level", "public"."products"."discontinued", "public"."products"."supplier_id" 
FROM "public"."products" 
WHERE to_tsvector(concat_ws(' ', "public"."products"."name")) @@ to_tsquery($1) 
OFFSET $2
Params: ["ve:*",0]

For Drizzle:

Query: 
select "id", "name", "qt_per_unit", "unit_price", "units_in_stock", "units_on_order", "reorder_level", "discontinued", "supplier_id" 
from "products" 
where to_tsvector('english', "products"."name") @@ to_tsquery('english', $1) 
-- params: ["ve:*"]

+100000