Select columns in firstOrCreate


I see that firstOrCreate makes a select * on table, wich can cause loss of performance.

Is there a way to say what columns we want firstOrCreate select?

There doesn’t seem to be straightforward method.

You can try to do something like this:

await stuff = Model.query().select('id').findOrCreate()

but I really doubt it works, since if I remember correctly findOrCreate() was Lucid thing, not Database.

In either case I wouldn’t be too worried about it. Since only remarkable performance drops will be if you hold huge blobs in database. Then networking between DB and API will get hit.

And if you start to hit those limits, then you can always rewrite it into Db.raw() or two queries Model.find() and model.create() yourself.

It doesn’t work.

Doesn’t seems smart run over 30 thousand queries selecting * instead of id (wich is the only thing I need).

So I’ll use Model.query().select(‘id’).findBy(‘name’,‘example’).

It would be so cool have a function that does that…I think I’ll do one myself.

For database when searching for data it does not matter if you do SELECT * or SELECT id. What you put in WHERE matters.

Performance wise it will be same if you have SELECT * FROM 'table' WHERE 'id' = 1 or SELECT 'id' FROM 'table' WHERE 'id' = 1 over table tables that have 1 row vs tables that have 300milion rows. Only time difference in table sizes will be in WHERE part, not in SELECT part.

And 30k rows is quite few, you shouldn’t have performance problems in there unless you have your indexes wrong. I’ve been dealing with databases with 3b rows max and some legacy code was doing SELECT * across that table without any problems.

We did drop SELECT * because table had huge text blobs that we did not need and droped it to reduce network load. Also there are some performance gains when not selecting blobs