title: query in firebase realtime database is very slow
body: <p>i have 30000 data in my realtime database ,the query takes a long time in first after that it being  fast.</p>
  <p>my rules:</p>
  <pre><code>{
    &quot;rules&quot;: {
      &quot;iBkHop2321&quot;:{
        &quot;$uid&quot;:{
          &quot;.read&quot;: &quot;false&quot;,
            &quot;.write&quot;: &quot;true&quot;
        }
      },
        &quot;contacts&quot;:{
          &quot;.read&quot;:&quot;root.child('iBkHop2321').child(auth.uid).val() === true&quot;,
            &quot;.write&quot;:&quot;root.child('iBkHop2321').child(auth.uid).val() === true&quot;
        }   
  }
  } 
  </code></pre>
  <p>my data:</p>
  <pre><code>{
    &quot;contacts&quot; : {
      &quot;-Moj9LK306LUDCXIbypG&quot; : {
           &quot;-Moj9LK5rgNYVcD1PJ2S&quot; : {
            &quot;android_id&quot; : &quot;312e62167b076ce1&quot;,
            &quot;mobile&quot; : &quot;770966855&quot;,
            &quot;name&quot; : &quot;عبدالله سيلان / فرع اب الشركة&quot;
            },
           &quot;-Moj9LKEZgZqu8tyW-4c&quot; : {
            &quot;android_id&quot; : &quot;312e62167b076ce1&quot;,
            &quot;mobile&quot; : &quot;711237507&quot;,
            &quot;name&quot; : &quot;عبد الملك عمر / الهلال للصرافة تعز&quot;
           },
           &quot;-Moj9LKFmA0E6jJMuWfD&quot; : {
            &quot;android_id&quot; : &quot;312e62167b076ce1&quot;,
            &quot;mobile&quot; : &quot;711290007&quot;,
            &quot;name&quot; : &quot;احمد هاشم /ايتي عدن للصرافة&quot;
           },
           .
           .
           .
      &quot;-MopGM8c8Fosxb0WKJpx&quot; : {
           .
           . 
      }
      .
      .
    &quot;iBkHop2321&quot; : {
      &quot;026Z0V8ad8MnJtnfn9qt6atgMWS2&quot; : true,
      &quot;0CDjvZBoWNO0zAxg8qt9M4n4Lgg2&quot; : true,
      &quot;10sILZEGXnP4v1kYwaeiFUZ1Ziw2&quot; : true,
      &quot;1QBZjbO5vmSNmmYwDcNmmpJ0AdN2&quot; : true,
      &quot;1p0dUGTEChXIqk0PfSZYVcPsSRD3&quot; : true,
      &quot;1ur5x65ZIeUpT38FTt1oZ6ICG383&quot; : true,
       .
       .
       .
       .  
  }
  </code></pre>
  <p>and this the query:</p>
  <pre><code>public void newSearch(String mobile){
      ListView listView = findViewById(R.id.contactsList);
      ArrayList&lt;Contacts&gt; arrayList = new ArrayList&lt;&gt;();
      ArrayContacts arrayContacts = new ArrayContacts(MainActivity.this, arrayList);
      listView.setAdapter(arrayContacts);
      showProgressDialogWithTitle(&quot;يرجى الانتظار&quot;);
      myRef.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot snapshot) {
              arrayList.clear();
              for(DataSnapshot ds : snapshot.getChildren()){
                  String uidKey  = ds.getKey();
                  assert uidKey != null;
                  DatabaseReference ref = myRef.child(uidKey);
                  Query query=ref.orderByChild(&quot;mobile&quot;).startAt(mobile).endAt(mobile+&quot;\uf8ff&quot;).limitToFirst(20);
                  query.addListenerForSingleValueEvent(new ValueEventListener() {
                      @Override
                      public void onDataChange(@NonNull DataSnapshot snapshot) {
                          if (snapshot.exists()) {
  
                              for (DataSnapshot snapshot1 : snapshot.getChildren()) {
                                  Contacts value = snapshot1.getValue(Contacts.class);
                                  arrayList.add(value);
                              }
                              ArrayContacts arrayContacts = new ArrayContacts(MainActivity.this, arrayList);
                              listView.setAdapter(arrayContacts);
                              hideProgressDialogWithTitle();
  
                          }
                          else {
                              hideProgressDialogWithTitle();
                          }
  
  
                      }
  
                      @Override
                      public void onCancelled(@NonNull DatabaseError error) {
                          Toast.makeText(MainActivity.this, &quot;children cancel&quot;, Toast.LENGTH_SHORT).show();
                      }
                  });
  
  
              }
  
          }
  
          @Override
          public void onCancelled(@NonNull DatabaseError error) {
              Toast.makeText(MainActivity.this, &quot;parent cancel&quot;, Toast.LENGTH_SHORT).show();
          }
      });
  }
  </code></pre>
  <p>when i lunch the app the Query is very slow after that the Query becomes very fast!</p>
  <p>please help me</p>
  <p><strong>Edit:</strong>
  the first data load takes longer than one min is this normal?
  Is my tree good and my rules?</p>