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

SQL query optimisations #617

Open
set-killer opened this issue Mar 13, 2015 · 0 comments
Open

SQL query optimisations #617

set-killer opened this issue Mar 13, 2015 · 0 comments

Comments

@set-killer
Copy link

Hello guys,
Lets, for example, see the current case. We have the following databases:

db.define('users', {
        id: Number,
        username: String,
        database: String
 });

db.define('address', {
        id: Number,
        user_id: Number,
        country: String,
        city: String,
        street: String
 });
db.models.address.hasOne('user', db.models.users, {reverse: 'addresses', autoFetch: true});

And now when i select ALL users it generates too many sql queries like this:

(orm/mysql) SELECT `id`, `username`, `database` FROM `users`
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 1
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 2
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 3
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 4
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 5
(orm/mysql) SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` WHERE `user_id` = 6

So basicly For every user in USERS creates a separate query from ADDRESS. Imagine if we have a milion users. This query will be soo slow.

Can this be optimized with 2 queries maybe like:

(orm/mysql) SELECT `id`, `username`, `database` FROM `users`
SELECT `id`, `user_id`, `country`, `city`, `street` FROM `address` 
WHERE `user_id` IN (1,2,3,4,5,6)

Or maybe with 1 query like:

SELECT `id`, `username`, `database` FROM `users`
LEFT JOIN `address` ON `address.user_id` = `users.id`

Thanks!

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

No branches or pull requests

1 participant