From 8b1efe58d24f16764af5e7164d54073b61d6d7ae Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 27 Dec 2019 19:18:54 +0330 Subject: [PATCH] better performance for exists() method of Query class https://github.com/yiisoft/yii2-mongodb/issues/290 --- src/Query.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Query.php b/src/Query.php index f1a8c2386..5dc0c1acc 100644 --- a/src/Query.php +++ b/src/Query.php @@ -495,7 +495,24 @@ public function exists($db = null) if (!empty($this->emulateExecution)) { return false; } + + #better performance + #save last options + $tmpOrderBy = $this->orderBy; + $tmpLimit = $this->limit; + $tmpOffset = $this->offset; + $tmpSelect = $this->select; + $this->orderBy = []; + $this->limit = 1; + $this->offset = null; + $this->select = ['_id']; $cursor = $this->buildCursor($db); + #return last options + $this->orderBy = $tmpOrderBy; + $this->limit = $tmpLimit; + $this->offset = $tmpOffset; + $this->select = $tmpSelect; + foreach ($cursor as $row) { return true; }