K
Kysely9mo ago
Ross

Subquery on same table

I'm experimenting with how to improve the performance of some queries against a PlanetScale MySQL DB. I have a fulltext index on a field in a table, then some other indexes on various other fields. MySQL can't use two indexes for a query so I'd like to try including the section of the query that uses the fulltext column in a subquery but I'm struggling with the syntax The primary key of the table is activityId. Something like
SELECT * FROM activities
WHERE someCol = '123'
AND someOtherCol = 'abc
AND activityId IN
(
SELECT activityId FROM activities
MATCH(bigTextField) AGAINST ("*some text*" IN BOOLEAN MODE)
)
SELECT * FROM activities
WHERE someCol = '123'
AND someOtherCol = 'abc
AND activityId IN
(
SELECT activityId FROM activities
MATCH(bigTextField) AGAINST ("*some text*" IN BOOLEAN MODE)
)
but I'm struggling with the Kysely syntax to achieve this, can anyone please point me in the right description?
3 Replies
Ross
Ross9mo ago
I've managed to achieve it by using the sql tag like this:
query.where(
'activityId',
'in',
sql`(SELECT activityId FROM activities WHERE MATCH(bigTextField) AGAINST (${value} IN BOOLEAN MODE))`,
);
query.where(
'activityId',
'in',
sql`(SELECT activityId FROM activities WHERE MATCH(bigTextField) AGAINST (${value} IN BOOLEAN MODE))`,
);
but I reckon there must be a better way...
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Ross
Ross9mo ago
Thanks, I'll have a play with that
Want results from more Discord servers?
Add your server
More Posts
How to return last inserted ID ?``` export async function createCommands(deviceId: number, commands: string[]) { return db .inhi, how do i make a select with a where that searches within a json? I'm using postgresinside the json there is an id that I want to use to do the search, but I didn't understand how to dCASE example in docs?Hello, where can one find an example of how to properly use CASE for query building? We want to useHow can I cast a jsonb value to a float8 in query?I have a query that has a jsonb value in the db that will always be a number, and I want to sum it. `InsertId` type for UUIDsHello, we use UUIDs as our primary IDs so when running insertInto statements we expected the insertIHow to access error events -- Is there a best/good practice for error handlingI'm writing my methods and associated tests for db access and when trying to validate error events. `createTable` function with dynamic column arrayHey folks, I'm trying to create a create table function; I feel like this should be straight forwardIssue Getting TypeScript to work with nested queryHi there 👋 . I'm experiencing an issue with getting typescript to play nice with Kysely. I should aHow can one make `jsonObjectFrom` respect nullability of underlying subquery?Hello, I am getting a type error when using the `jsonObjectFrom` helper on a subquery where the baseFiltering data based on relationship valueHello, I'm using subqueries to fetch and return related data from a resource table. I am using the `