How to handle simultaneous data request and response in mysql database? - mysql

The problem scenario is:
I have a database table 'User_Datails_Table' with fileds(id,username, luckyNumber).
All logged in users can updated their 'luckyNumber' as they wish. Also a user has option to view all the existing user details. There is an option to sort(DESC) the user details based on 'luckyNumbers'.
The requirement is such that I need to query the database with LIMIT values. For example,query fetching 1st two records(LIMIT 0,2), then next query fetching next 2 records(LIMIT 2,2) so on.
Consider the database contains 4 record:
id username luckyNumber
1 USER1 67
2 USER2 66
3 USER3 64
4 USER4 63
Two user(USER1 and USER3) are logged in.
If USER3 fetches the first two records using LIMIT 0,2( result is USER1 and USER2) and USER1 changes his luckyNumber to 62(simultaneously).
Now if USER3 fetches the next two records using LIMIT 2,2( result is USER4 and USER1). So USER1 gets repeated in the result.
I use php to fetch the mysql database. How can such a scenario be handled? Does mysql handle this automatically?

Related

mySQL Messaging Between Two Users

I have a private messaging system that uses a mysql database.
Messages are passed between two users.
However, when any user deletes the conversation history, it should not be deleted in the other user.
I could create an Additional column called "deleted_users" and use LIKE when listing messages.
But I'm worried about performance and I need your help.
id
user_from
user_to
msg
1
82
85
test
2
85
82
test

Does MySQL compress query responses?

I am currently trying to optimise some DB queries that get run a lot, the queries are run by using a SELECT query against a view, this view does a lot of joins. I thought I might be able to speed things up by caching the results of the view into a table and selecting from the table instead of the view.
Let's say I have 2 tables
People:
PersonId
Name
1
Anne
2
Brian
3
Charlie
4
Doug
CustomerPeople:
CustomerId
PersonId
1
1
1
2
1
3
1
4
2
1
2
2
and I have a view that joins the two tables to give a list of people, by name, belonging to the customer:
CustomerId
PersonName
1
Anne
1
Brian
1
Charlie
1
Doug
2
Anne
2
Brian
When I query the view, I look at the Duration/Fetch and it is 0.10 sec/4.00 sec
I decide to cache the view data into a table and create a new table:
CustomerNamedPeople
CustomerId
PersonName
1
Anne
1
Brian
1
Charlie
1
Doug
2
Anne
2
Brian
Which contains the exact same data, however now when I query the table, I look at the Duration/Fetch and it is 0.05 sec/6.00 sec
My understanding is the Duration is the time it takes MySQL engine to run the query, and Fetch is the time it takes the data to be returned to the client (over the network). Unsurprisingly the Duration was faster, and took only 50% of the time, which makes sense, there is no longer a join occurring, however the Fetch took 150% of the time, and is slower.
My question here is: Does MySQL do some sort of response stream compression, since it knows that Anne and Brian are repeated, it can send them only once and have the client "decompress" the data?
The reason I ask is because I am doing something similar but with 1,000,000 rows returned, the data in the two responses is identical, but the view Fetch takes 20 seconds, and the table Fetch is 60 seconds, most of the PersonNames are repeated more than once, so I am wondering if perhaps there is some sort of compression occurring in the response, should I not expect MySQL to take the same time to Fetch two sets of identical data?

Query for extracting info from one table based on other

I have two tables post and share, post has many share. I want to fetch all data in post table using userId(posted by owner user) and also check share table using same userid that is if some one shared post to other user, if any condition is true, I need to fetch data.
I want to fetch data in post table if posted by owner or shared by other user in share table.
Example :
table name: post
id(pk) postname userid
1 abc 10
2 xxx 10
3 yyy 11
4 zzz 12
5 bbb 13
table name:share
id postid(fk) userid
1 3 10
2 4 10
3 3 11
4 1 12
Expected output: example find by userid 10
id postname userid
1 abc 10 // this record created by user 10 (owner)
2 xxx 10 // this record created by user 10 (owner)
3 yyy 11 // this record shared by other user to user 10.
4 zzz 12 // this record shared by other user to user 10.
You may want to print created_by and shared_by in two different columns as the example seems a bit confusing. Below query should produce the expected output in that case:
select p.id, p.postname, p.userid as 'created_by', s.user_id as 'shared_by'
from post p left outer join share s on p.id = s.postid
where p.userid = 10
order by p.id;
It finally clicked what you are looking for..
select p.id, p.postname, p.userid
from post p
join share s on s.postid=p.id
where s.userid='10' or p.userid='10'
That should return all interactions related to a specific user (id 10), either if a post was created by that user, or a post was shared to that user.

Count the number of rows that have a specific piece of data?

I have the following table
id Desc User
1 Print 14
2 Print 7
3 Copy 14
4 Print 19
5 Copy 7
6 Copy 19
7 Attach 19
What I'm trying to do is make a column that tells the number of rows per user.
Like this
id User Count
1 14 2
2 7 2
4 19 3
The Point of the report is to show how many activities each user has done.
I need to group by user and get the number of rows within each user.
The problem is, I'm not exactly sure how to do that, is it a unique statement somewhere?
Here's my query so far.
Select id
,User
From Table
Group By User
I am unsure how to implement the count though.
You should be able to get your result with the COUNT function:
SELECT
MIN(id),
User,
COUNT(User) AS Count
FROM
`table`
GROUP BY
User
Because you can get only one id value per User I assumed from your data that you want the minimum one.

SSRS Report /SubReport / Multipe Tables or Datasets in Single Report

I have a table Project.
ProjID Proj_task
1 NIT 2.0
2 SSRS
I have table called Project_Task.
ProjID Task_DS User
1 task1 User1
1 task2 User2
1 task3 User3
2 task4 User4
2 task5 User5
2 task6 User6
I want to generate Report in Below format. (Each project ID inforamtion shouldbe in one Page for which i am using Insert Group )
In Page 1:
ProjID : 1
Proj_task: NIT 2.0
--------------------
User Task_DS
User1 task1
User2 task2
User3 task3
In Page 2:
ProjID : 2
Proj_task: SSRS
--------------------
User Task_DS
User4 task4
User5 task5
User6 task6
I tried creating two tables, subreports, Joining two tables & creating single datasets to achive the same.
I am not able to arrive at the above mentioned format. Can Anyone please help me how can i do that?
If you want to repeat a set of items under each Project ID , you need to put sub report inside the group..
Pass the ProjectID as the Parameter to the Sub Report
So, the subreport has to run for each and every project ID . So, Create a group by PKGID and then Place that sub report inside the group so that it repeats for each Project ID
Hope this helps..If you needany furthur details..you can ask me.
Can you please provide query , for furthur details..
Updated:
Select distinct Proj_ID , Proj_name from dbo.Project
This should be your first data set
select TASK_RSLV_DS, TASK_IMPCT_DS from dbo.PROJECT left join dbo.PROJ_TASK on PROJECT.PROJ_ID = PROJ_TASK.PROJ_ID
This should be your second dataset
Pass the Project ID as a parameter to Subreport
Note: You have create a group in the main report design inorder to loop your sub report.
for clear explanation:
Use this link:
Here, follow the steps in creting drill down report
http://www.codeproject.com/Articles/195017/SSRS-Series-Part-II-Working-with-Subreports-DrillD#4
Hope this helps..