Should I convert my MySQL database to JSON file? - mysql

I created a quiz website, the questions and answers are fixed, the data usually does't change.
When a user loads a quiz page, a query is made to the MySQL database to retrieve the question data (including answer, related image data etc). There are about 2000 questions total in the database. The query to the database is made based on the question's unique ID.
I would like to speed up the page loading time. I've read about making a query to the MySQL database and then converting the data into JSON format, but that seems like it would make the process longer. Should I convert the MySQL database into a single JSON file, then have the website's quiz page directly query the JSON file rather than the MySQL database to grab the question data?

There's no need for JSON here unless you want to make AJAX requests
This is a common scenario for webapps. The following may be a one of many approaches (assuming MySQL and PHP):
1. Load the single question page with $questionID in the URL (and read it with $_GET['questionID']) or you pass it as $_POST
2. Query the database with:
SELECT *
FROM questions
WHERE question_id = "(int)$questionID"
LIMIT 1
3. Build your HTML with the info returned by your query
4. Display your info to the user
5. On $_POST check for right/wrong answers against answers table querying by $questionID and $answerID (you might need to loop thru them)
6. Store the results in $_SESSION so you can show final results on the last page.
Hope that helps,

Related

How to get the 10000 locations from database in java based on performance wise?

How to get the 10000 locations from database in java based on performance wise
In my date base location table have 10000 locations is there,
iam getting the locations based on hibernate query("from Locaion") in sping controller
but my view page loading time is very slow
how it solved
pls tell me
my code is:
List<Location> locations= mservive.getlocatins();
Query query = getSession().createQuery("from Location");
List<Location> loc=query.list();
return loc;
I'm using auto complete option using in loading for locations in view layer
Fetching 1000+ locations in a single query is never going to be efficient like that. You need to look at paging the data, or change your database design.
I am not sure exactly how your DB is made, but if you really need this kind of data, better option is to place it in a single column ( like XML) , fetch it in a single row and then process that in Java.
Which DB you are using?
Is your data static or going to change?
How often do you need to change this data?
The design can be changed once we consider the above mentioned questions.
If its not possible to change the design, look at paging, fetch only the number of records needed to display on the page, and let the remaining come later when user clicks next.
Hope this helps.

How to Create simple View or query subdocument

I read the docs - but can't seem to get my head around this ( I'm a SQL guy )
1) I loaded a json file in using CBdocloader
[
{
"ID": "9e78f4a6-4061-48aa-8154-0b738d93461b",
"More fields": ""
}
]
2) There is now an object in my bucket calles values100 ( that was the name of the file ) .
3) How to I access the data in this object in the bucket that I imported through a query or view?
Select * from mybucket returns 1 result that has all the rows I loaded - but I really want to query that data in that bucket? Should I create a view? Should I query a View? My Question is #3 but I am confused..
I believe there are a couple of things going on:
a. cbdocloader expects that each document is contained in a separate file. The behavior desired indicates that each document should instead be its own file, rather than one single document on Couchbase. The tool will then create multiple couchbase documents which can be indexed. I'm not sure there is a way to split out the text file using the tool; you may have to write a script to do it for you.
b. Couchbase is intended to be a document database, not a SQL database. As such, the way you would access a document in a majority of the cases is via the document id, which should have some significance to your application. This is not to say you can't look up the document id in an index, but you may find a SQL database to work better if you plan to do a lot of complex queries. If you need help on creating an index, please post a new question.

Neo4J custom load CSV

I asked a question a few days ago to know how to import an existing database into Neo4J. Thanks to the person who explained me how to do that. I decided to create a CSV file from my database (around 1 million entries) and to load it from the Neo4j webadmin to test it. The problem is that each row of this database contains redundant data, for example my database contains actions from different users but each user can do mutliple actions. The structure of my graph would be to create a node for each user that is linked to each action he does. That's why I have to create only one node for each user even if his name appears in several rows of my CSV file (because he made several actions). What is the method to do that ? I guess it's possible to do that in Cypher right ?
Thanks a lot
Regards
Sam
In case you have references that might or might not exist, you should use the MERGE statement. MERGE either finds something or creates something in your database.
Please refer to the respective section in the reference manual: http://docs.neo4j.org/chunked/stable/cypherdoc-importing-csv-files-with-cypher.html. Here the country is shared my multiple users there the country is merged wheres the users and their relationships to countries are unconditionally created.

Correct foreigen key approach in jpa Or data saving basic approach

This question of mine is subjective
i am getting a list of objects from a third site.
now i want to save that data in database.
suppose the data is List. This response is to a query that i fired to that site .
now i want to save two things
1) query name
2) the response(List) (answer)
the myobject can have lot of answers corresponding to my query. now i want to save all these answers separately so that each answer can be fetched independently.
now i have this DB approach
one table for query and query id
second table which will consist of query id and query answer. (which will be foreigen key in first table
My question is am i following right approach?
initially i thought of saving the whole list in database but as per my knowledge we can not save list in database directly although in jpa implementation 2.0 we can save list in db (correct me if i am wrong)
please guide me with my current approach or of there is any better approach
i am using JPA 2.0 eclipselink.
Regards
Anil Sharma
What is your object model?
You can use OneToMany or ManyToMany to store a collection of Entity objects.
If you have a List or List you can store this using an ElementCollection.
But you may be better off creating an Answer or AnswerReference Entity.
See,
http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

Data Model for Profile Page Views

Say I have a site with user profiles that have publicly accessible pages (each profile has several pages each). I'd like to show the users page view statistics (e.g. per page, for a certain time period, etc.). What's a good way to store page views?
Here's what I was thinking:
Table Page Views
================
- Id (PK)
- Profile Id (FK)
- Page Id (FK)
- Timestamp
I'm afraid this solution won't scale. Suggestions?
Your intuition is correct, writing to a database doesn't scale particularly well. You want to avoid a database transaction for each page request.
That noted, is scaling really your concern? If so, and assuming a Internet site (as opposed to intra), skip rolling your own and collect the hit data with Google Analytics or something similar. Then take that data and process it to generate totals per profile.
However, if you're really hellbent on doing it yourself, consider log parsing instead. If you can enumerate the URLs per profile, use that information, and your web server logs, to generate hit totals. Tools such as Microsoft's Log Parser, which can process A LOT of different formats, or *nix command line tools like sed and grep are your friends here.
If enumeration's not possible change code to log the information you need and process that log file.
With logs in place, generate results using a batch process and insert those results into a database using MySQL's LOAD DATA.
Final note on the roll your own approach I've recommended - this will scale a lot better if you have a clustered environment than database transaction per request.
It depends on what kind of reports you want to make available.
If you want to be able to say "this is the list of people that viewed your page between these two dates", then you must store all the data you proposed.
If you only need to be able to say "your page was viewed X times between these two dates", then you only need a table with a page ID, date, and counter. Update the counter column on each page view with a single UPDATE query.
I suppose you can have
tblPerson
personid(pk)
activeProfileID(fk) -- the active profile to use.
timestamp
tblPage
pageid(pk)
data
tblPersonProfile
profileID(pk)
timestamp
tblProfilePages
profilePageID(pk)
profileid(pk)
pageid(pk)
isActive