i am using Codeigniter i am facing an issue while setting the values of "Limit" in a query,Limit is only showing "limit NULL"
Here is the snippet of my code.
SELECT block.loc, owner.name , block.dist_name FROM house INNER JOIN block ON house.block_id = block.block_id INNER JOIN owner ON owner.house_id = house.house_id WHERE
block.dist = ? AND house.status = 5 limit ? , ?
$result = $this->db->query($qry, array($this->getDist(), (int) $this->getLimitStart(), (int) $this->getLimitOffset()));
dump for
(int) $this->getLimitStart() is '0' and (int) $this->getLimitOffset() is '10'
As I understand that you make your own getter setter of the object, the getter which u are providing in you query, is returning NULL is only because you are not using the same setter.
For Example:
if You use it ($this->getLimitOffset()) you have to set it also like this yourObject->setLimitOffset(10). I think it will work for you now.
You should swap the start and offset values like this
SELECT block.loc, owner.name , block.dist_name FROM house INNER JOIN block ON house.block_id = block.block_id INNER JOIN owner ON owner.house_id = house.house_id WHERE
block.dist = ? AND house.status = 5 limit ? , ?
$result = $this->db->query($qry, array($this->getDist(), (int) $this->getLimitOffset(),(int) $this->getLimitStart()));
Becsuse codeigniter active record limit's first parameter is limit abd second is offset.
http://codeigniter.com/user_guide/database/active_record.html
Try this: $this->db->limit($nrecords, $offset);
Related
I originally had an SQL statement, this:
SELECT *, COUNT(friend_one) AS pending_count , COUNT(friend_two) AS requests_sent
FROM friends
WHERE friend_one OR friend_two = ?
AND status = ?
In which I assigned my parameters like :
$pending_friend_count_stmt->execute(array($user_id, $status_one));
However, the query was not getting the results I wanted. Someone showed me a different way of doing it, but it has the variable $user_id in it multiple times, so I do not know how to adjust the code to be able to use a parameter.
You can see the new query here:
http://rextester.com/KSM73595
Am I able to just do
SELECT COUNT(CASE WHEN `friend_one` = ? THEN 1 END) as `requests_count`,
COUNT(CASE WHEN `friend_two` = ? THEN 1 END) as `pending_count`
FROM `friends`
WHERE ? IN ( `friend_one` , `friend_two` )
AND `status` = ?
$pending_friend_count_stmt->execute(array($user_id, $user_id, $user_id $status_one));
Using PDO you have the ability to use named parameters, however in your question you want to use 1 parameters for multiple values and that means emulation has to be on:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Now you can do the following:
$stmt = $db->prepare("SELECT * FROM table WHERE userid = :userid AND userid = :userid");
$stmt->excecute([
':userid' => 1
]);
Resulting in:
"SELECT * FROM table WHERE userid = 1 AND userid = 1"
I have this MySQL query which I am loading in to my home controller and after running Codeigniter's $this->output->enable_profiler(TRUE); I get an execution time of 5.3044
The Query inside my model:
class Post extends CI_Model {
function stream($uid, $updated, $limit) {
$now = microtime(true);
$sql = "
SELECT
*
FROM
vPAS_Posts_Users_Temp
WHERE
post_user_id = ?
AND post_type !=4
AND post_updated > ?
AND post_updated < ?
UNION
SELECT
u.*
FROM
vPAS_Posts_Users_Temp u
JOIN
PAS_Follow f
ON f.folw_followed_user_id = u.post_dynamic_pid
WHERE u.post_updated > ?
AND post_updated < ?
AND (( f.folw_follower_user_id = ? AND f.folw_deleted = 0 )
OR ( u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by = ? AND u.post_user_id != ? AND u.post_type =4 ))
ORDER BY
post_posted_date DESC
LIMIT ?
";
$query = $this->db->query($sql, array($uid, $updated, $now, $updated, $now, $uid, $uid, $uid, $limit));
return $query->result();
}
}
Is there anything I can do here to improve the execution time and therefore increase my page load?
Edit
Explain Results
MySQL Workbench Visual Explain
Maybe you won't believe it, but DON'T retrieve SELECT * in your SQL. Just write the fields you want to retrieve and I think it'll speed up a lot.
I've seen increases in speed of more than 20 times when executing a query (from 0.4secs to 0.02 secs) just changing * for required fields.
Other thing: If you have an auto_increment id on INSERT in your tables, DON'T use post_posted_date as ORDER field. Ordering by DATETIME fields is slow, and if you may use an INT id (which hopefully you will have as an index) you will achieve the same result quicker.
UPDATE
As required in the question, technical reasons:
For not using SELECT *: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. This is for SQL, but for MySQL (not as complete as question before) mySQL Query - Selecting Fields
For Ordering by Datetime: SQL, SQL Server 2008: Ordering by datetime is too slow, and again, related to MySQL: MySQL performance optimization: order by datetime field
Bonus: Learning how to set the indexes: http://ronaldbradford.com/blog/tag/covering-index/
I would add indexes on post_user_id, post_updated and folw_follower_user_id.
In this case it may also be better to not use union and separate the query into two separate ones and then use PHP to combine to two result sets.
If you switched to using active record you could also look into caching, to get better performance
The rows column shows an estimate for how many rows needs to be examined, which as I understand, means that in your case, it has to scan 72 * 1 * 2627 * 1 * 2 rows, which is quite a lot.
Now, the trick is to bring down this number, and one way is to add indexes. In your case, I would suggest adding an index which contains:
post_user_id, post_type, post_updated, post_updated.
This should bring down the first result set, of 72 rows.
Now for the UNION, try using UNION ALL instead, as it is claimed to be faster.
If that doesn't fix the problem, I would suggest rewriting the query to not use a UNION call at all.
Try the query with left join as you are trying to union on same table
"SELECT
u.*
FROM
vPAS_Posts_Users_Temp u
LEFT JOIN PAS_Follow f ON f.folw_followed_user_id = u.post_dynamic_pid
WHERE (
u.post_user_id = ?
AND u.post_type !=4
AND u.post_updated > ?
AND u.post_updated < ?
)
OR
(
u.post_updated > ?
AND post_updated < ?
AND (( f.folw_follower_user_id = ? AND f.folw_deleted = 0 )
OR ( u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by = ? AND u.post_user_id != ? AND u.post_type =4 ))
)
ORDER BY
u.post_posted_date DESC
LIMIT ?"
I think you can remove the UNION from the query and make use of left join instead and avoid the unnecessary conditions:
SELECT U.*
FROM vPAS_Posts_Users_Temp AS U
LEFT JOIN PAS_Follow AS F ON F.folw_followed_user_id = U.post_dynamic_pid
WHERE U.post_updated > ?
AND U.post_updated < ?
AND (
(
F.folw_follower_user_id = ? AND F.folw_deleted = 0
)
OR
(
U.post_passed_on_by = F.folw_follower_user_id OR U.post_passed_on_by = ?
)
)
ORDER BY
U.post_posted_date DESC
LIMIT ?
Also identify and set proper indexes in your tables.
I have googled my problem but didnt get the answer.
I want to list all of the results of below sql including NULL (when COUNT(review.id) return 0 also) but instead i just got the results of articles of place that only contains review.
$sql = "SELECT tbl_place.id, tbl_place.region_id, tbl_place.subregion_id, tbl_place.title, tbl_place.metalink, tbl_place.img_thumbnail, tbl_place.summary, tbl_place.category1_id, tbl_place.category2_id, tbl_place.category3_id, COUNT(review.id) AS total_review FROM tbl_place
JOIN review ON tbl_place.id = review.place_id
WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1' AND
review.rating != '0.00'
GROUP BY tbl_place.id
ORDER BY total_review $by
LIMIT $limit OFFSET $offset";
please use left join for review table instead of join. join is by default inner join so it will take only matched records.
the sql should be :
$sql = "SELECT tbl_place.id,
tbl_place.region_id,
tbl_place.subregion_id,
tbl_place.title,
tbl_place.metalink,
tbl_place.img_thumbnail,
tbl_place.summary,
tbl_place.category1_id,
tbl_place.category2_id,
tbl_place.category3_id,
(SELECT COUNT(*) FROM review WHERE review.rating != '0.00' AND tbl_place.id = review.place_id ) AS total_review
FROM tbl_place WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1'
GROUP BY tbl_place.id
ORDER BY total_review $by";
it's working! thx guys!
I want to update the values of one column in a table from '0' to '1', if either of four values in columns in another table are '1'. Somehow this doesn't seem to work and I was just wondering if anyone could help me get the code right or find a different way of doing it if it's not possible,
mysql_query("UPDATE members
INNER JOIN forum_banners ON members.id = forum_banners.userid
SET members.beta = '1' WHERE forum_banners.bebeta = '1' OR
forum_banners.bibeta = '1' OR forum_banners.cbeta = '1' OR
forum_banners.wbeta = '1'") or die(mysql_error());
That's what I tried, but it's not working, I suspect because of the OR. I tried having all updatings in different mysql_query bits, but that didn't work either.
You should be able to update from multiple table references. This is untested, but gives you an idea:
UPDATE
members, forum_banners
SET
members.beta = '1'
WHERE
members.id = forum_banners.userid
AND forum_banners.bebeta = '1'
OR forum_banners.bibeta = '1'
OR forum_banners.cbeta = '1'
OR forum_banners.wbeta = '1'
http://dev.mysql.com/doc/refman/5.0/en/update.html
Note "Multi-Table syntax"
Try
UPDATE
m
SET
m.beta = '1'
FROM
members m
INNER JOIN
forum_banners fb
ON m.id = fb.userid
WHERE
fb.bebeta = '1'
OR fb.bibeta = '1'
OR fb.cbeta = '1'
OR fb.wbeta = '1'"
Aliases also help make your syntax a little neater.
I'm having some issues declaring a value in my php statement. The 2 values outside of the sub query declare fine but the one inside the sub query does not work and gives me Undefined index: deviceName.
$sql4 = "SELECT endPort, startPort,
(
SELECT deviceName
FROM devices
WHERE devices.deviceID = patching.endDeviceID
)
FROM patching
WHERE deviceID = '$deviceID'
LIMIT 0 , 10";
$query4 = mysql_query($sql4);
while ($row = mysql_fetch_array($query4))
{
$startp = $row['startPort'];
$endp = $row['endPort'];
$endname= $row['deviceName'];
echo '<tr class="logm"><td>'.$startp.'</td><td></td><td>'.$endname.'</td><td></td><td>'.$endp.'</td><td></td></tr>';
}
Is it possible to declare this value and am i doing it wrong or is there another way round this issue.
You didn't give the subquery result a name:
SELECT endPort, startPort,
(
SELECT deviceName
FROM devices
WHERE devices.deviceID = patching.endDeviceID
) AS deviceName
FROM patching
WHERE deviceID = '$deviceID'
LIMIT 0, 10
Though you should be using joins, not subqueries:
SELECT endPort, startPort, deviceName
FROM patching
LEFT JOIN devices ON(devices.deviceID = patching.endDeviceID)
WHERE patching.deviceID = '$deviceID'
LIMIT 0, 10
You should use a join for this:
SELECT endPort, startPort, deviceName
FROM patching
LEFT JOIN devices USING (deviceID)
WHERE deviceID = '$deviceID'
LIMIT 0 , 10
Note: If $deviceID is user input, you have to escape it with mysql_real_escape_string or use prepared statements.