Shantanu's Blog
Database Consultant
July 25, 2005
MySQL Case Study - 43
How to speed up the query where OR is being used?
select * from table where (
( field1 = 'VALUE1' and field2 like 'VALUE2%' )
OR
( field3 = 'VALUE1' and field2 like 'VALUE2%' )
)
I have created two composite indexes - one on field1 field2 and one on field3 field2. The query takes an age to run and looking at my log indicates a full table scan.
I have also tried indexing just field1 and field3 separately but this
doesn't help.
July 19, 2005
MySQL Case Study - 42
I have a table:
create table example(time datetime, username varchar(255));
time username
2005-06-21 15:58:02 user1
2005-06-21 14:58:02 user1
2005-06-21 11:57:51 user2
2005-06-21 10:57:51 user1
2005-06-21 09:57:51 user1
The query:
select COUNT(*), username, MAX(time) as maxtime from example group by
username order by maxtime desc;
groups by username, and returns:
COUNT(*) username maxtime
4 user1 2005-06-21 15:58:02
1 user2 2005-06-21 11:57:51
I want it, however, to return:
COUNT(*) username maxtime
2 user1 2005-06-21 15:58:02
1 user2 2005-06-21 11:57:51
2 user1 2005-06-21 10:57:51
That is, do not group entries by the same user together if another
user visited in between. Is this possible?
July 14, 2005
A recent study by London Business School found that, in a typical developing country, a rise of ten mobile phones per 100 people boosts GDP growth by 0.6 percentage points. Mobile phones are, in short, a classic example of technology that helps people help themselves.
http://www.economist.com/printedition/displayStory.cfm?Story_ID=4157618
July 13, 2005
MySQL Case Study - 41
I have a table that has 3 fields: id (auto-increment, primary key), parentId (integer, default 0) and sum (double)
"Normal" rows just have and id and a sum, but then there can also be childs to those rows, and child rows have the parent row's id set in the parentId-field
Example
Code:
id | parenId | sum
1 0 120.00
2 0 23.00
3 1 12.00
4 1 20.00
5 4 10.00
So in this example rows 1 and 2 are top-level rows. Rows 3,4 are childs to row 1 and row 5 is child to row 4. The child rows' sums are always <= compared with the parent row.
I'm trying to build a select query that selects all the info in the table. My problem is that if a row has childs, I want the sum of child row's sum to be reduced from the parent row's sum. eg when returning row 1, it's sum should be 88.00 (120.00-12.00-20.00). Row 4's sum should be 10.00 (20-10).
Can I do the subtraction using a subquery?
http://forums.devshed.com/t269837/s.html
MySQL Case Study - 40
review_id relates the vote to a specific review.
table 1 (reviews)
id, title, message
12 | John's Review of Something | The review goes here
table 2 (votes)
id, review_id, vote
3 | 12 | 6
4 | 12 | 10
8 | 12 | 8
the vote column is between 1 and 10 inclusive. In this case the total vote average for review_id 12 is (6+10+8)/3=8 and there might be another whose average is say 8.5. I want to be able to figure out which has the highest.
What I want is to be able to retrieve the id and title etc of the item in reviews with the highest avg votes.
http://forums.devshed.com/t269755/s.html
July 07, 2005
No subqueries in the from clause
MySQL have recognised that this particular problem is a design flaw. The select statement (while creating a view) cannot contain a subquery in the from clause, it is possible however to use subqueries in other parts of the view definition.
create view v_emp as select * from (select emp_id from emps) as emps;
ERROR 1349 (HY000): View’s SELECT contains a subquery in the FROM clause
create view v_emps as select * from emps where dept_id in
(select dept_id from dept);
Query OK, 0 rows affected (0.03 sec)"
http://mysql.gilfster.com/page.php?parent_id=3&page_id=3.0.5
July 05, 2005
http://www.db4free.net/* You will need to register (free) before you can start using.
* Can be used for critical business applications.
Frequently Asked Questions...
I want to access my database account from a script which is hosted at my Internet Service Provider. Which hostname must I use?
Ans: The hostname is db4free.org (Attention: NOT db4free.net!)
And your username, password and database name.
What can I enter in the field 'IP address'?
Ans: You can restrict access to your database account to a specific IP address or host to increase the security of your account.
For example, if you only want to access your database account via db4free's phpMyAdmin, you can enter 'localhost'. Or if you want to limit access to your PHP files that are hosted at your ISP, you can enter your ISP's hostname or IP address. Entering a more specific value than the percent sign (which gives you access from any host), makes it impossible to access your account from anywhere else (including hackers).
How many database accounts can I have?
Ans: You can create 2 database accounts per email address.
MySQL Case Study - 39
I'm looking to list the number of Users who have an entry matching 'Page View' in the table UserActions. The query I'm using is:
SELECT COUNT(*) AS Download_Ruequests
FROM (SELECT *
FROM UserActions,
(SELECT *
FROM UserActions
WHERE ACTION LIKE "View%"
GROUP BY UserID) AS Users
WHERE UserActions.ACTION = "Request Download"
GROUP BY UserActions.UserID) AS Downloads
However, it involves 2 subqueries and I can't help but feel there must be a simpler way...
I want to return users who have viewed a page AND requested a download, but each of these actions is in a different row in the table. So I'm effectively saying, show me the User IDs which have this action and another action in a different row.
Any pointers greatly appreciated.
Answer:
http://forums.mysql.com/read.php?10,31794,31836#msg-31836
SELECT COUNT(DISTINCT ua.UserID)
FROM UserActions ua
INNER JOIN (SELECT UserID
FROM UserActions
WHERE ACTION = 'Request Download') AS uad
ON ua.UserID = aud.UserID
INNER JOIN (SELECT UserID
FROM UserActions
WHERE ACTION = 'Page View') AS uap
ON ua.UserID = aup.UserID
MySQL Case Study - 38
First of all this is what i have so far
SELECT s_c.s_id, s_c.course_id, s_c.start_date, s_c.complete_date, s_c.due_date, count(s_c_u.unit_id) AS total_units, count(s_c_u.complete_date) AS complete_units
FROM s_c
LEFT OUTER JOIN s_c_u
ON s_c.course_id = s_c_u.course_id
WHERE s_c.s_id = xxxx
GROUP BY s_c.course_id
This selects the desired fields from table s_c (a table that contains which student does which course) then computes the total units (from the s_c_u table) that the student has in the corrisponding course and how many they have completed. But what i'd also like to do is figure out how many units are overdue based on a date that i feed in. There is a field due_date in the s_c_u table. So i need something like
[Pseudo code]
count(s_c_u.due_date) AS overdue_units WHERE due_date < date
but im not sure how to write such a query that integrates into the query above. I hope it's possible, otherwise i'll have to do it with php and it wont be very efficient.
July 02, 2005
MySQL Case Study - 37
I have a db with this table
CREATE TABLE `customers` (
`idcustomer` int(10) unsigned NOT NULL auto_increment,
`Customer Name` varchar(45) NOT NULL default '',
PRIMARY KEY (`idcustomer`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `customers` (`idcustomer`, `Customer Name`) VALUES (NULL, 'OLDCustomer');
If I execute this operation
START TRANSACTION;
lock table customers WRITE;
UPDATE `customers` SET `Customer Name`='MODIFYcustomer' WHERE `idcustomer`=1;
unlock table;
ROLLBACK;
SELECT * FROM `customers`;
'idcustomer','Customer Name'
'1','MODIFYcustomer'
The record changes. Is it correct? Or is it a bug?
If I execute the same operation with a different order the record doesen't change
lock table customers WRITE;
START TRANSACTION;
UPDATE `customers` SET `Customer Name`='AAAAAcustomer' WHERE `idcustomer`=1;
ROLLBACK;
unlock table;
SELECT * FROM `customers`;
'idcustomer','Customer Name'
'1','MODIFYcustomer'
Archives
June 2001
July 2001
January 2003
May 2003
September 2003
October 2003
December 2003
January 2004
February 2004
March 2004
April 2004
May 2004
June 2004
July 2004
August 2004
September 2004
October 2004
November 2004
December 2004
January 2005
February 2005
March 2005
April 2005
May 2005
June 2005
July 2005
August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
February 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
November 2006
December 2006
January 2007
February 2007
March 2007
April 2007
June 2007
July 2007
August 2007
September 2007
October 2007
November 2007
December 2007
January 2008
February 2008
March 2008
April 2008
July 2008
August 2008
September 2008
October 2008
November 2008
December 2008
January 2009
February 2009
March 2009
April 2009
May 2009
June 2009
July 2009
August 2009
September 2009
October 2009
November 2009
December 2009
January 2010
February 2010
March 2010
April 2010
May 2010
June 2010
July 2010
August 2010
September 2010
October 2010
November 2010
December 2010
January 2011
February 2011
March 2011
April 2011
May 2011
June 2011
July 2011
August 2011
September 2011
October 2011
November 2011
December 2011
January 2012
February 2012
March 2012
April 2012
May 2012
June 2012
July 2012
August 2012
October 2012
November 2012
December 2012
January 2013
February 2013
March 2013
April 2013
May 2013
June 2013
July 2013
September 2013
October 2013
January 2014
March 2014
April 2014
May 2014
July 2014
August 2014
September 2014
October 2014
November 2014
December 2014
January 2015
February 2015
March 2015
April 2015
May 2015
June 2015
July 2015
August 2015
September 2015
January 2016
February 2016
March 2016
April 2016
May 2016
June 2016
July 2016
August 2016
September 2016
October 2016
November 2016
December 2016
January 2017
February 2017
April 2017
May 2017
June 2017
July 2017
August 2017
September 2017
October 2017
November 2017
December 2017
February 2018
March 2018
April 2018
May 2018
June 2018
July 2018
August 2018
September 2018
October 2018
November 2018
December 2018
January 2019
February 2019
March 2019
April 2019
May 2019
July 2019
August 2019
September 2019
October 2019
November 2019
December 2019
January 2020
February 2020
March 2020
April 2020
May 2020
July 2020
August 2020
September 2020
October 2020
December 2020
January 2021
April 2021
May 2021
July 2021
September 2021
March 2022
October 2022
November 2022
March 2023
April 2023
July 2023
September 2023
October 2023
November 2023
April 2024
May 2024
June 2024
August 2024
September 2024
October 2024
November 2024
December 2024