How to improve LIKE query with multiple where methods

I have this query,

User.where('name LIKE ?', "%#{term}%").where(group: "student").limit(10)

and I have this sneaking suspicion that I can improve it somehow, mainly allowing on one where method instead of two but any attempt results in the sql query not recognizing the group field. So far the above works but I want to know if this can be improved or not and how.

Also, should I worry about having multiple where methods in a query besides for the aesthetic look?


The current code you have is fine and is more readable than combining the calls to where. As pointed out by other commenters/answers, having multiple calls to where will not impact the total speed of the db query.

If you're wondering how to change the query to a single where call, try the following

User.where('name LIKE ? AND group = ?', "%#{term}%", 'student').limit(10)

The multiple where's are not going to have any noticeable impact on the performance. The DB is only ever being hit once.

Queries with "LIKE" are inherently slow.

Need Your Help

How can I get aggregate counts without repeating first column in group by clause?

sql-server-2008 tsql group-by pivot i2b2

This query is forcing me to put f.CONCEPT_CD in the group by clause because I'm using it in the case statements in the select clause. How do I keep the counts in the result set, without repeating

Programmatically restart a Windows Service

c++ windows-services

I know this can be done in C#/.Net but I was wondering whether it can be done calling the Windows API?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.