What's the best way to work with complex queries? it is sometimes difficult to use Eloquent to query multiple tables
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
In Laravel applications we can write queries by different methods i.e., e.g., Eloquent, Simple Query and Raw Query, I want to learn which type of method is fast for data base operations
All of these eventually use the same way of querying and the speed difference is negligible.
But if you really want to know, I would suppose Eloquent is the slowest, closely followed by the query builder. and a raw query will be the fastest as there is no query building to be done.
My advice would be to use what fits best in your project, the time difference will be a few microseconds per request.
So when we use Eloquent and When DB facades:
When we'll work on a simple and small records site with simple CRUD and there records are not fact, then use Eloquent there. When we'll work on a lot's of records, it is better to use DB Query than Eloquent. So, finally it is cleared that - when we'll use Database Query and When we'll use Eloquent Query.
We use frameworks like Laravel to ease our work. When it comes to concept of frameworks, speed and ease of development is more important than performance. So by using ORM’s it provides powerful methods to handle with database without no need of heavy mysql knowledge. In case of query builder it’s provide methods to build queries in efficient manner.
As my experience there are some things that we can’t easily do with Eloquent. So then we need to use query builder to build direct queries.
So I think it’s not good to compare Eloquent and query builder.
You can find more details here.
But I can give you some tips to select one method.
If you are more aware of efficiency rather than ease of development, go for query builder.
If you dealing with one entity go for ORM, (Eloquent).
If you dealing with multiple entities it’s better to deal with query builder.
If you are new to mysql or your application is not very complex, definitely choose ORM.
If you need more complex query, I recommend to use query builder.
I need to search over multiple tables, getting only one resultset, but these tables don't have any relationship.
Can I do it with Solr?
Maybe Solr's aliases might fit your needs.
Create your alias with :
http://[solr.host:port]/solr/admin/collections?action=CREATEALIAS&name=testalias&collections=[collection1],[collection2]
and then query as usual as if "testalias" was one collection. For example:
http://[solr.host:port]/solr/testalias/select?q=:&rows=100&wt=json&indent=true
This query will return all the data from both collections.
I have multiple migrations (say around 10) and the corresponding models for the tables. The problem I am facing is that all the migrations/tables have multiple primary keys but the model has just a string variable to define primary key ($primaryKey), hence, when I save or update a table row using where clause it would just take one primary key and miss the other and hence end up changing multiple rows instead of one. So, basically I switched to DB Queries and it worked well.
So, my question to you is that is there any performance gain with model? Or it is just a designing paradigm? Is there any option to do the same thing (Have multiple primary key) within a model? I know by overriding the Eloquent methods we can do this but is there any other good option?
Using an ORM like Eloquent is a convenience, not a requirement. From what I've heard there's actually often a slight performance decrease when using an ORM because there's the additional overhead of translating that query into the relevant SQL.
Often, using an ORM makes for queries that are much easier to understand, and for that reason alone it's worth using. However, for more complex queries, an ORM is likely to get in the way, and so you should consider using another method to query the database. You don't have to use just an ORM - you can mix and match the different methods as you see fit.
Laravel has the Query Builder as an alternative to writing raw SQL or using the ORM, and that may be a better option for you here. I would avoid writing raw SQL if you can because both Eloquent and the Query Builder will handle escaping the parameters for you, to help avoid SQL injection vulnerabilities.
My choice would be to use the ORM where possible, and fall back to the Query Builder when the ORM gets in the way.
Does anyone has the data of speed comparison between Eloquent ORM, Query Builder, and Raw SQL Queries? What is the better choose?
Raw SQL will always be the fastest because a human can always optimize the code and script to their liking. The query builder (aka Fluent) will be the next fastest, only slightly slower than Eloquent. That's because Eloquent uses Fluent within itself along with its own models and relationships.
If you're looking for pure processing speed, use raw SQL.
Otherwise use Eloquent for fastest development UNLESS you have no models and relationships, then use Fluent.
I would like to apply a Criteria query to an in-memory collection
of entities, instead of on the database. Is this possible?
To have Criteria API work like LINQ? Or alternatively, convert
Criteria query to LINQ query.
Thanks!
I don't believe you can use Criteria to query against an in-memory collection and come to think about it it doesn't seem to make much sense. If I'm understanding everything correctly you've already queried against your database. I'd suggest to either tune your original query (whichever method you choose) to include all of your filters. Or you could use LINQ (as you suggested) to refine your results.
Also, what's your reasoning for wanting to query from memory?
It sounds like you're rolling your own caching mechanism. I would highly recommend checking out NHibernate's 2nd level cache. It handles many complex scenarios gracefully such as invalidating query results on updates to the underlying tables.
http://ayende.com/Blog/archive/2009/04/24/nhibernate-2nd-level-cache.aspx