I have a question about Aerospike DB , and secondry indexes.
I have set of students , and each student (record key is StudentId),
has a map (bin) , that consists of <CourseId ,Grade>
.
I want to select only students that participated specific coursesIds . How can I do it ? Should I add secondry index on the map? Using UDF?
Thanks.
You could use two approaches - one with secondary indexes, and one without (just key-value operations using the primary index).
Without Secondary Indexes
Let's actually assume you have three types at your application, each in its own set in Aerospike - courses
, students
, roster
. The course object holds information about the course, the student objects hold information about the student.
The roster
objects have the same key as the course, or maybe a composite courseID|semester. The course should have a bin students which holds a list of student IDs. To get the students of a course you get the roster
record, then turn its list of student keys into a single batch-read operation against students
.
Of course you could also collapse this and just have two sets - courses
and students
and keep the list of course students as a bin in the course object. No need for a many-to-one join here, since you have the list and map complex data types to work with.
If you wanted to do more complex queries without a secondary index, you can apply a predicate filter onto a scan of the courses
set (See an example of using the Java client this way).
With Secondary Indexes
You can add a bin in-course to the student objects, with its value being a list of course IDs (meaning that you can easily get the course object from the ID). You can build a secondary index over that list of course IDs. To get the student's courses you get the student object by its key, and look at the in-course bin. To get all the students in a specific course, run a secondary index query over that bin of the records in the students
set.
You can apply a predicate filter to the records found by this secondary index query, to further filter the students by other criteria.