Efficient algorithm for searching in database - database

I am working on a new project where I have 500k questions to be stored in database. Each time a student answers a question, that ques is marked under visited category for that specific user. Thus, what a student should see on main page is question that he has never visited. So, my problem is that if 10 questions are posted on the main page, then each question will have to be first matched with collection of the visited question, check whether they are in the list of visited question for that user and then get posted on the main page. How should I minimize the time required for such kind of requirement? or Is my algorithm inefficient?

Related

Query elements in differnet orders without doublons in Firestore

I'm implementing a quizz and want to fetch questions from a Firestore.
How can I query questions one by one from the DB in a different order between players but being sure that I never show twice the same question to the same player.
Is it possible to give an array of IDs that I already fetched ? Or is there another solution that can help me ?
There is nothing built in to Firebase for that. The first approach that comes to mind would be to track what questions you've already asked, and find a next question at random with the approach described here. Then if you've already used that question, find another random one.

Database Design for Conditional Questionnaire

I am designing a database schema to support a business case in which a user can submit a request (for him/herself or on someone else's behalf). To process and complete the request, the submitter will be prompted with questions based on their answers to prior questions. That is to say, the next question is conditional based on the current question's answer.
Each question will have an associated type, which will drive the user form for that particular question. A question of type boolean indicates Yes/No radio buttons for the answer. Questions of type multiple indicates a multiple choice answer, where users will select one of multiple radio options.
I have two questions:
How can I modify my schema to "link" answers to multiple choice questions? (ie "the following answers are available for question X.")
How should the answers drive the next question? (ie. "for question #1, if answer A is chosen, then GOTO question 5")
My question_relationships table will let me specify that question 1 is the parent of question 5, and question 5 is the parent of question 6. But I really need the answers to drive this logic.
question
-id
-question_name
-question_text
-question_hint
-question_type (boolean, multiple)
question_relationship
-id
-fk_parent_question_id
-fk_child_question_id
request
-id
-person_id
-submitter_id
-submit_date
-status
request_answer
-id
-fk_request_id
-fk_question_id
-answer_text
-answer_boolean
I have seen the answers in db design - survey/quiz with questions and answers, but I believe that my scenario is a bit different.
A table has an associated fill-in-the-(named-)blanks statement aka predicate. Rows that make it into a true statement go in the table. Rows that make it into a false statement stay out. That's how we interpret a table (base, view or query) and update a base. Each table represents an application relationship.
(So your predicate-style quote for 2 is how to give a table's meaning. Because then JOIN's meaning is the AND of argument meanings, and UNION the OR, EXCEPT is the AND NOT, etc.)
How can I modify my schema to "link" answers to multiple choice
questions? (ie "the following answers are available for question X.")
// question [question_id] has available answer [answer_id]
question_answers(question_id, answerid)
How should the answers drive the next question?
(ie. "for question #1, if answer A is chosen, then GOTO question 5")
// for question [this_id] if answer [answer_id] is chosen then go to question [next_id]
next_question(this_id, answer_id, next_id)
PS
There are many different ways of representing graphs (nodes with edges between them) via tables. Here the nodes are questions and the edges are this-next question pairs. Different tables support different kinds of graphs and different patterns of reading and update. (I chose one reflecting your application, but framed my answer to help you find your best representation via proper design yourself.)
PPS
If different user traces through questions can mean that which question follows another is context-dependent:
// in context [this_id] if answer [answer_id] is chosen then go to context[next_id]
next_context(this_id, answer_id, next_id)
What a "context" is depends on aspects of your application that you have not given. What you have given suggests that your only notion of context is the current question. Also, depending on what a context contains, this table may need normalization. You might want independent notions of current context vs current question. (Topic: finite state machines.)

what should be database schema for online voting app

I want to develop an app where Events/Question would be posted by admin and user will vote or answer. Here question can be of three different type and each type can have different option.Admin can view reports about each question
E.g
WHQuestion: What is right age for marriage? (1) >20 (2)=20 (3)20<
Voting:Who is best captain (1)ABC (2)PQR n so on......
YesNoQuestion: Is Dhoni a good captain? (1)Yes (2)No
So I am confused here about the database schema and tables. How should i manage them?
All questions are multiple choice with one or zero correct answers. So: One question, some answers, one optional correct answer.
Question: question_no, text
Answer: question_no, answer_no, text
As to how to store which answer per question is correct, there are two options:
Store the answer_no in the question record. I consider this the better option. A dbms featuring deferred Constraints (so a question record can reference an answer record and vice versa) would be a good thing to have here. If there is no correct answer the answer_no is null.
Have a flag in the answers table and then mark one answer per question as correct and the others as incorrect. This would be appropriate if there were multiple correct answers per question possible. For one correct answer, however, this would be the worse option of the two. To guarantee data consistency you would apply some special check, which can be a bit complicated (for instance a function index to guarantee uniqueness). For no correct answer you could store the same value or even null for all answers. However, you must see the answers to find out that this is a vote question. So again, option 1 is the better choice where you see this immediately in the question record.

Is it possible to make a booking system using delphi/lazarus? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am programming a database(DB) in Lazarus as a project and for increased complexity have NOT downloaded any additional libraries. The software is intended to allow customers to check availability of hiring a villa and them make a booking if the villa is available.
Declaring records and writing them to a file acts as a table for my DB. Reading/Writing/Deleting the records in my files has been achieved successfully and I now move on to the point where I use this data to make bookings.
I have 3 tables:
Clients
Villas
Bookings
Now, my problem comes in with the bookings table. How do I make my application know that a villa has already been booked for the period in which a new booking wants to be made. (basically double booking shouldn't be allowed) So far as mentioned, I can only read/write and delete records in my tables and now move on to the booking stage. Please ask if further info is needed
I'm thinking of using the Tcalendar but have no idea how to program with it or even if that is the simple way of doing it. Any tips please?
You have to solve two distinct problems:
Implement logic (not visible to the user) that determines whether a requested booking is available or whether the villa is booked for some or all of the requested period.
Implement some kind of visual display for the user to see when a villa is booked.
TCalendar would only help you with the second part, which is the least interesting (because you don't really need a visual interface, you could simply pop up a message that says "Villa Not Available").
To write the logic that will tell you whether a booking is available, you will need to refine your data model (or, if you've done that already, explain it to us in more detail). Specific questions you need to address are:
Do you issue bookings against individual villas ("I want to book the Butterfly House for one week starting July 1") or against an inventory of identical villas ("I want to book one of your two-bedroom villas for one week starting July 1").
You need to decide how you're going to store the booking information. When I've tried a task like this in the past, I've found it easiest to store a record for each night of each reservation then, for a request, do a separate query to determine if I can satisfy that individual night. A request for which I can satisfy all nights is bookable.

Logic for recommender application

I am developing an application - which would have users answer maybe 10 questions - which would have 3-4 options for each question. At the end of the 10th question, based on the responses, it would need to suggest a certain solution. Since there are 100's of permutation and combinations - what's the logic that would be required to use and the database design,
thanks
EDIT some more detailed explanation
if my application is used to recommend a data plan from various mobile operators - based on the user answering questions like the time spent on the internet, the type of files being downloaded and so on. So, if the response to question 1 was a and question 2 was c, etc - then it would be a certain plan. If the response to question 1 was b and for question 2 it was c, then it would recommend a certain plan. So, if there were 10 questions - then the combinations can be quite large. So is there a certain algorithm that can handle this?
I. what would be the logic?
If I understand correctly, you would define "rules" such as
If the answer to question 5. is either A or B then the suggested plan would be planB, otherwise execute the rest of the rules.
So you would use a rule engine e.g.: http://www.jboss.org/drools/
II. what would be the database design?
This is quite simple:
USERS table,
QUESTIONS table and
ANSWERS table which would refer to the two others
Possibly there would be a QUESTIONNAIRE table as well, and the QUESTIONS table would refer to it.
Just a 'quick' comment, consider letting the user see changes in what company they could be recommended as they answer every question.
For example, if I am most interested in price that would be the question I would answer first and immediately see the 3 cheapest plans/products recommended to me.
The second question could be coverage and if I then could see the 3 plans with best coverage (in my area) that would be interesting too.
When I answer the third question about smart phone features and I say I want internet, then the first question should spit out the 3 cheapest plans/products that include internet, obviously they could change.
And so on...
Maybe it also could be a good idea to let the user "dive into" each question and see the full range of options for that answer. As a user I would appreciate that.
Above comments is just how I would appreciate if a form was made for me, I don't want to answer 10 questions about stuff I'm not really putting any value on, each user is different and will prefer to make their choice on their questions.
So, based on above it would be like a check list where the top answers would be the plans/products with the most fitting check marks. And to give immediate responses (as the user answer/alter each question), here AJAX would probably be your choice.

Resources