I wrote this query for retrieving all the public events in Milan:
search?fields=name,venue&type=event&q=milan
But I need to query the city field in Venue. Is it possible?
$fql_query = 'https://graph.facebook.com/search?q=milan&type=event&'.$access_token;
This fql query will search all events that have a word "milan"
Related
I am working in Dynamo DB for the first time . My assignment is Ticket Management System where it has 3 entities Department , User and Ticket. The relationship between each entity is.
I have identified the following access patterns
Fetch a Department.
Fetch all users in Department
Fetch a given user in Department
Fetch all Tickets belongs to the Department
Fetch all Tickets assigned to the User
for which i defined the following data model . I am thinking of creating GSI with Tickets as PK and User as SK to do 4 & 5
On a higher level I need to perform 2 updates . I can update the User to which the ticket is assigned and I can update the ticket status as inprogress, resolved . And in the table I have Ticket details as JSON object as below.
I need help from from the experienced people whether my understanding and approach is efficient.
I think you're on the right track. I'd design it as a table with two Global Secondary indexes. The base table looks like this:
The first Global Secondary Index like this (GSI1):
The second Global Secondary Index like this (GSI2):
Now for the why:
This design allows you to easily update the following things:
A user's department
A ticket's status if you know the ticket Id
A ticket's user if you know the ticket Id
A ticket's department if you know the ticket Id
You can get a bunch of information from this model:
Fetch a Department.
Query the base table with the department name or list all departments
Fetch all users in Department
Query GSI 1 with the Department Name and filter the sort Key using begins_with = USER#
Fetch a given user in Department
Sound like you know the UserId, so do a GetItem on the base table. If that's not the case, do the query mentioned in "Fetch all users in Department".
Fetch all Tickets belongs to the Department
Query GSI 1 with the department name as the PK and filter the SK using begins_with = Ticket#
Fetch all Tickets assigned to the User
Query GSI 2 with the user id as the PK and filter the SK using begins_with = Ticket#
I have this Patient model with fields: company, sex, is_alive...
In the patient search page, I have checkboxes for these fields. So if users want to search all male alive patient in a company ... they will tick on the checkbox.
Then in the backend, in views.py, I collect values of checkboxes and put into a list called "value_list". Next, I do the query search like this:
patient_list = Patient.objects.all()
if "abc" in value_list:
company = Company.objects.get(name="abc")
patient_list = patient_list.filter(Q(company=company))
if "alive" in value_list:
patient_list = patient_list.filter(Q(is_alive=True))
if "male" in value_list:
patient_list = patient_list.filter(Q(sex=MALE))
My question is if this way of filtering is efficient and good practice.
Please advise.
Thank you.
In terms of performance, everything looks fine. You will end up executing 1 query with this code because Django is smart enough to know to combine these into 1 large query that uses several subqueries.
That being said, you can massively simplify this with the __in clause, which is simply a shorthand for multiple or conditions:
Patient.objects.filter(company__in=Company.objects.filter(name__in=values_list)))
I have a table of log entries that have user_id and datetime. I'd like to make a query to fetch the most recent of each log entry by user_id. I can't seem to figure out how to do that...
The SQL for the query would be something like this:
SELECT *
FROM table a
JOIN (SELECT user_id, max(datetime) maxDate
FROM table
GROUP BY user_id) b
ON a.user_id = b.user_id AND a.datetime = b.maxDate
Right now I'm doing it with a raw query, but I'd like to use the ORM's methods.
I suppose this should do
Table.objects.order_by('-user_id').distinct('user_id')
See distinct() in this -> https://docs.djangoproject.com/en/1.9/ref/models/querysets/
But this will only work if the latest log entry for that user is the last entry by that user in the table, i.e., the log entries of a particular user as sorted in ascending way in the table.
You may try:
User.objects.latest().id
Table.objects.order_by('user_id', '-datetime').distinct('user_id')
Add indexes to user_id and datetime (Meta.index_together = ['user_id', 'datetime']).
i want to fetch all the records against a specific user_id, in my table user data is in serialize form in a column (user_data).
a:9:{s:9:"auth_type";s:4:"user";s:7:"user_id";s:2:"40";s:9:"user_type";s:1:"2";s:5:"fname";s:9:"Saif Alvi";s:5:"sname";s:0:"";s:8:"nickname";s:8:"saifalvi";s:6:"gender";s:0:"";s:13:"user_type_str";s:8:"employer";s:7:"lang_id";s:1:"1";}
to fetch all records against user_id i am using below query
$result = $this->DB->select("COUNT(*) AS sessionCount")->from(SESSIONS_TABLE)->where('user_data', REGEXP.'s:7:"user_id";s:"^[0-9]+":'.'"$user_id"')->get()->result_array();
where i am wrong or any other solution to use codigniter active record where() with serialize data and regex
I have two entities, Video and Vote. Vote has a many-to-one relationship with Video, but Video has no relationship with Vote. I'm trying to retrieve a list of Videos sorted by vote count.
The following SQL works to get me what I want:
SELECT video.*, COUNT(video_id) AS vote_count
FROM video
LEFT JOIN vote ON vote.video_id = video.id
GROUP BY video.id
ORDER BY vote_count DESC;
I'm trying to achieve something similar with DQL, but no luck so far:
SELECT vid.name, COUNT(vote.video_id) as vote_count
FROM VideoVote\Video\Video vid
JOIN vote.video vid
GROUP BY video.id
ORDER BY vote_count DESC
It can't be done because ... video does not have a relation to vote. So your options are
Make the relationship bidirectional (i would go for this one). No penalty on the database, little extra overhead in PHP. (Test performance to see if it works for you).
Use your native SQL query and use result set mapping.
Thank you Flip. Once I added the one-to-many association from Video to Vote, the following DQL works:
SELECT vid, COUNT(vote.id) AS HIDDEN vote_count
FROM VideoVote\Video\Video vid
LEFT JOIN vid.votes vote
GROUP BY vid.id
ORDER BY vote_count DESC