i have Big Data has nearly 1 million records and more , the query takes a long time in fisrt query And most of the time.
my Rules:
{
"rules": {
"iBkH2321":{
"$uid":{
".read": "false",
".write": "true"
}
},
"Contacts":{
".indexOn": ["mobile","name","android_id"],
".read":"root.child('iBkH2321').child(auth.uid).val() == true",
".write":"root.child('iBkH2321').child(auth.uid).val() == true",
},
"AppVersion":{
".read": "true",
".write": "false",
".indexOn":["VersionNumber"]
}
}
}
an data like this:
{
"contacts" : {
"-Moj9LK5rgNYVcD1PJ2S" : {
"android_id" : "312e62167b076ce1",
"mobile" : "770966855",
"name" : "عبدالله سيلان / فرع اب الشركة"
},
"-Moj9LKEZgZqu8tyW-4c" : {
"android_id" : "312e62167b076ce1",
"mobile" : "711237507",
"name" : "عبد الملك عمر / الهلال للصرافة تعز"
},
"-Moj9LKFmA0E6jJMuWfD" : {
"android_id" : "312e62167b076ce1",
"mobile" : "711290007",
"name" : "احمد هاشم /ايتي عدن للصرافة"
},
.
.
.
.
.
}
"iBkHop2321" : {
"026Z0V8ad8MnJtnfn9qt6atgMWS2" : true,
"0CDZ0V8aAxMnJtnre9qt6atgZiw2" : true,
.
.
.
.
}
and this my code:
private DatabaseReference myRef ;
.
.
.
final FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Contacts");
auth = FirebaseAuth.getInstance();
.
.
.
private void TestShearch(String number){
AtomicBoolean getResult = new AtomicBoolean(false);
ArrayList<Contacts> arrayList = new ArrayList<>();
ArrayContacts arrayContacts = new ArrayContacts(context, arrayList);
listView.setAdapter(arrayContacts);
Query query = myRef.orderByChild("mobile").equalTo(number).limitToLast(30);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(isAdded() && activity != null) {
arrayList.clear();
getResult.set(true);
for (DataSnapshot ds : snapshot.getChildren()) {
Contacts con = ds.getValue(Contacts.class);
arrayList.add(con);
assert con != null;
}
arrayContacts.notifyDataSetChanged();
listView.setAdapter(arrayContacts);
} else {
getResult.set(false);
Toast.makeText(context, "لا توجد بيانات لهذا الرقم حالياً", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.GONE);
btn_search.setVisibility(View.VISIBLE);
myRef.removeEventListener(this);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
//do somthing here
}
};
query.addListenerForSingleValueEvent(valueEventListener);
}
note: am using authentication in onStart():
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void updateUI(FirebaseUser currentUser) {
if(currentUser == null){
mAuth.signInAnonymously().addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
myRef2.child(Objects.requireNonNull(mAuth.getUid())).setValue(true);
Log.d("userAuth11", "signInAnonymously:parent success");
} else{
Log.d("userAuth11", "signInAnonymously:parent failed");
updateUI(null);
}
}
});
}
when i launch the app the query takes longer than 30-60 sec after that becomes faster than old query!
can you help me? thanks.
I think that the problem is in the size of the data that I inquired from, as the size of the data is approximately 2 gigabytes?