简体   繁体   English

给定查询之间的性能差异?

[英]Performance difference between given queries?

I'm trying to make a method that build a RealmQuery . 我正在尝试制作一种构建RealmQuery的方法。

But here's my problem. 但这是我的问题。 I wonder what's performance difference between given queries? 我想知道给定查询之间的性能差异是什么?


1. Selecting all records with WHERE clause. 1.使用WHERE子句选择所有记录。

SELECT * FROM table WHERE gender = 'M' OR gender = 'F';
Realm.where(SOME_CLASS.class)
    .equalTo("gender", "M")
    .equalTo("gender", "F")
    .findAll();


2. Selecting all records with WHERE ~ IN. 2.使用WHERE〜IN选择所有记录。

SELECT * FROM table WHERE gender IN ('M', 'F');
Realm.where(SOME_CLASS.class)
    .in("gender", new String[] {"M", "F"})
    .findAll();


3. Selecting all records without WHERE clause. 3.选择所有不带WHERE子句的记录。

SELECT * FROM table;
Realm.where(SOME_CLASS.class)
    .findAll();

Note that column gender contain M or F only. 请注意,列gender仅包含MF
I think all of those queries return same result, but I want to know how does it works internally and what's performance between those queries. 我认为所有这些查询都返回相同的结果,但是我想知道它在内部如何工作以及这些查询之间的性能如何。

Thanks for your interesting and sorry for my bad English. 感谢您的有趣和抱歉,我的英语不好。

The following two conditions are functionally identical: 以下两个条件在功能上是相同的:

WHERE gender = 'M' OR gender = 'F'
WHERE gender IN ('M', 'F')

Both the first two queries would require checking the gender value for each record. 前两个查询都需要检查每个记录的性别值。 Since every record is a match, it should not make much difference whether or not there is an index as the full table will be hit regardless. 由于每个记录都是匹配项,因此是否有索引应该没有多大区别,因为无论如何都会命中整个表。 The third query, with no WHERE clause, also will do a full table scan, but it will not have to do any checking in the WHERE clause. 没有WHERE子句的第三个查询也将执行全表扫描,但不必在WHERE子句中进行任何检查。 For this reason, all other factors being the same, I would expect the third query to outperform the first two. 因此,在所有其他因素相同的情况下,我希望第三个查询的性能优于前两个查询。

But the firm answer to your question can be had by analyzing these queries on the database using EXPLAIN or a similar tool. 但是,可以通过使用EXPLAIN或类似工具在数据库上分析这些查询来获得对您问题的肯定答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM