Which one is Faster, SUB-Query or IN-Query? - mysql

If I want to select whether i am following the threads or not.
I have two approaches to do so... but I don't know which one would be more better in terms of performance and speed. Can anyone help me out?
Approach 1
$cui = $_SESSION['user_id'];
$data = mysqli_query($con,"
SELECT t.*,( select count(follow_id) from follows where object_id =
t.thread_id AND object_type='thread' AND user_id = $cui) as me_follow FROM threads t
");
while($row = mysqli_fetch_assoc($data)){
/*
$row['me_follow'] = 0 if i aint following
$row['me_follow'] = 1 if i am following
*/
}
Approach 2
$cui = $_SESSION['user_id'];
$data = mysqli_query($con,"SELECT * FROM threads");
$ids = array();
while($row = mysqli_fetch_assoc($data)){
$ids[] = $row['thread_id'];
}
$ids = join($ids,",");
$data = mysqli_query($con,"SELECT COUNT(*) FROM follows WHERE object_id IN($ids) AND user_id = $cui");

One round-trip wins over two. This is because there is some overhead in sending SQL to the server, having it parse it, execute it and send the results back. It is (usually) better to do everything in one round-trip.

Related

Selecting next related row in MySQL

I have a spatial dataset in MySQL 5.7 where I have the columns: id, deviceid, latlng_point, time. latlng_point is a geospatial Point.
What I'm trying to achieve is calculating distance from points. I'm unsure on how to approach this.
SELECT
ST_DISTANCE_SPHERE(latlng_point, i want the next latlng_point here) AS distance
FROM points
WHERE deviceid = 1
ORDER BY time DESC;
In PHP I would do something like this:
<?php
$conn = new mysqli($host,$user,$pass,$db);
$query = "SELECT latlng_point FROM points WHERE deviceid = 1...";
$latlng_array = array();
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
$latlng_array[] = $row;
}
}
$distance = 0;
for ($i = 0; $i < count($latlng_array) - 1; $i++) {
$pt1 = $latlng_array[$i]['latlng_point'];
$pt2 = $latlng_array[$i+1]['latlng_point'];
$distance += haversine_function($pt1,$pt2);
}
echo "Distance: {$distance}";
?>
I'm trying to achieve something similar purely in MySQL.
Try this one:
SELECT SUM(ST_DISTANCE_SPHERE(p1.latlng_point, p2.latlng_point)) AS total_distance
FROM points p1
JOIN points p2 ON p2.id = (
SELECT p.id
FROM points p
WHERE p.deviceid = p1.deviceid
AND p.time > p1.time
ORDER BY p.time ASC
LIMIT 1
)
WHERE p1.deviceid = 1
The (correlated) subquery should return the id of the next point (sorted by time).
I can't tell you if it is really efficient or if it even works at all (can't test it).
However you should have an index on (deviceid, time) - Assuming that id is the primary key.

How to convert this query to doctrine DQL

SELECT apntoken,deviceid,created
FROM `distribution_mobiletokens` as dm
WHERE userid='20'
and not exists (
select 1
from `distribution_mobiletokens`
where userid = '20'
and deviceid = dm.deviceid
and created > dm.created
)
What this query does is selects all mobiletokens where the user id is equal to 20 and the deviceid is the same but chooses the newest apntoken for the device.
My database looks like below.
For more information on this query, I got this answer from another question I asked here(How to group by in SQL by largest date (Order By a Group By))
Things I've Tried
$mobiletokens = $em->createQueryBuilder()
->select('u.id,company.id as companyid,user.id as userid,u.apntoken')
->from('AppBundle:MobileTokens', 'u')
->leftJoin('u.companyId', 'company')
->leftJoin('u.userId', 'user')
->where('u.status = 1 and user.id = :userid')
->setParameter('userid',(int)$jsondata['userid'])
->groupby('u.apntoken')
->getQuery()
->getResult();
//#JA - Get the list of all the apn tokens we need to send the message to.
foreach($mobiletokens as $tokenobject){
$deviceTokens[] = $tokenobject["apntoken"];
echo $tokenobject["apntoken"]."\n";
}
die();
This gives me the incorrect response of
63416A61F2FD47CC7B579CAEACB002CB00FACC3786A8991F329BB41B1208C4BA
9B25BBCC3F3D2232934D86A7BC72967A5546B250281FB750FFE645C8EB105AF6
latestone
Any help here is appreciated!
Other Information
Data with SELECT * FROM
Data after using the SQL I provided up top.
You could use a subselect created with the querybuilder as example:
public function selectNewAppToken($userId)
{
// get an ExpressionBuilder instance, so that you
$expr = $this->_em->getExpressionBuilder();
// create a subquery in order to take all address records for a specified user id
$sub = $this->_em->createQueryBuilder()
->select('a')
->from('AppBundle:MobileTokens', 'a')
->where('a.user = dm.id')
->andWhere('a.deviceid = dm.deviceid')
->andWhere($expr->gte('a.created','dm.created'));
$qb = $this->_em->createQueryBuilder()
->select('dm')
->from('AppBundle:MobileTokens', 'dm')
->where($expr->not($expr->exists($sub->getDQL())))
->andWhere('dm.user = :user_id')
->setParameter('user_id', $userId);
return $qb->getQuery()->getResult();
}
I did this for now as a temporary fix, not sure if this is best answer though.
$em = $this->em;
$connection = $em->getConnection();
$statement = $connection->prepare("
SELECT apntoken,deviceid,created
FROM `distribution_mobiletokens` as dm
WHERE userid=:userid
and not exists (
select 1
from `distribution_mobiletokens`
where userid = :userid
and deviceid = dm.deviceid
and created > dm.created
)");
$statement->bindValue('userid', $jsondata['userid']);
$statement->execute();
$mobiletokens = $statement->fetchAll();
//#JA - Get the list of all the apn tokens we need to send the message to.
foreach($mobiletokens as $tokenobject){
$deviceTokens[] = $tokenobject["apntoken"];
echo $tokenobject["apntoken"]."\n";
}

MySQL query involving multiple rows, access one based on max value

So I am trying to have a single script that accesses all values of a table based on a particular id. The script returns the values in an array using PHP:
Example:
// Select data from DB
$query = 'SELECT * FROM experiences WHERE user_id = ' . $id;
$result = mysqli_query($link, $query) or die (mysqli_error($link));
// Not sure this actually creates a 2D array...
$array = mysqli_fetch_array($result);
However, I realize that I need modified results for particular tasks, such as the row where a particular value is the highest.
How would I go about doing this, and does $array actually hold all the rows and respective fields?
TRY
'SELECT * FROM experiences WHERE user_id ='.$id.' HAVING MAX(column_name)
OR
"SELECT max(column_name), other column...
FROM experiences
WHERE user_id =".(int)$id
Assuming you have cleaned $id properly you can do this
// Select data from DB
$query = 'SELECT * FROM experiences WHERE user_id = ' . $id;
$result = mysqli_query($link, $query) or die (mysqli_error($link));
$data = array();
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
and $data will contain the 2D array you're looking for.
If you want the row where a particular value is highest I suggest either looking into ORDER BY or MAX() depending on your needs.

merge multiple sql queries

In the header of the pages of my site, I have multiple SQL queries. For example:
$sql_result = mysql_query("SELECT blah2 FROM misc WHERE id='1'", $db);
$rs = mysql_fetch_array($sql_result); $blah2 = $rs[blah2];
$sql_result = mysql_query("SELECT * FROM ip_bans WHERE IP='$ip'", $db);
if (mysql_num_rows($sql_result) != 0) { header("Location: banned.php"); }
$sql_result = mysql_query("SELECT * FROM accounts WHERE username='$un' AND pw='$pw'", $db);
$rs = mysql_fetch_array($sql_result);
// do all account login check etc
$sql_result = mysql_query("SELECT * FROM members WHERE id='$id'", $db);
$rs = mysql_fetch_array($sql_result);
// get members data
..and more.
All this info is needed for each page of my site. Is there a way to combine these, or just limit the ammount of queries? I'd imagine this would get quite intensive on the database over time.
I'm not too good with JOINs and things, is that what i need?
You want to use UNION.
Here is a good tutorial: http://www.tizag.com/sqlTutorial/sqlunion.php
I would recommend you to only use it where it's "natural" and not merge to many different queries.

Mysql client ran out of memory

When i try to combine 3 tables having 50K records and write a MySQL select query:
select t1.c1,t2.c2 from table1 t1,table2 t2,table3 t3
where t3.column3='<value>' and t1.column1=t2.column1
and t2.column2=t3.column2
and t2.column2='<value1>' or t2.column2='<value2>'
This is the kind of the query which am writing to run
I get "mysql client ran out of memory"
Any help on how to overcome this will be highly appreciated.
Thanks
What client do you use? For mysql you can try running it with --quick option
Here's what I wrote to get around the memory limit. You'll have to modify the limits to match your environment. I'm breaking my results into a 500 per record batch and processing that and then looping into the next $records_per_batch.
<?php
// variables
$counter = 0;
$records_per_batch = 500;
$total_records = 0;
$app_root = "/var/run/consumers";
// mark the start time
system("/bin/touch $app_root/clean.last_start");
$link = mysqli_connect("localhost", "username", "password", "database") or die(mysqli_error());
// get the total amount of records to process
$sql = "SELECT COUNT(*) from table";
$result = mysqli_query($link, $sql) or die("couldn't execute sql: $sql" . mysql_error());
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
$total_records = $row[0];
}
// iterate through the records at $records_per_batch at a time
$sql_template = "SELECT * FROM table order by table_id limit %s, %s";
while ($counter < $total_records) {
$sql = sprintf($sql_template,$counter,$records_per_batch);
$result = mysqli_query($link, $sql) or die("couldn't execute sql: $sql" . mysql_error());
if ($result) {
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
// do your work here.
}
}
} else {
print "hmm, no result\n";
exit;
}
$counter += $records_per_batch;
}
mysqli_close($link);
system("/bin/touch $app_root/clean.last_end");
?>
I know this post is old but I think it's worth mentioning that the or is probably causing an issue as well.
Here it is with some joins, and specifying the or for the t2 value only, which is what I think you want. This should limit your result set.
select t1.c1,t2.c2
from table1 t1
inner join table2 t2 on t2.column1=t1.column1
inner join table3 t3 pm t2.column2=t3.column2
where t3.column3='<value>'
and t2.column2 IN('<value1>','<value2>');
50K records is not too large to give you the type of problem described. You can try optimizing your tables by indexing on columns that join or limit such as t2.column1, t2.column2, etc.