473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trouble With Counting New Documents With Complex Query


I need help width formulating the most
effective (in terms of processing time)
SQL query to count all the "new"
documents in the repository, where "new" is
defined as "from 00:00:01 up to 23:59:59
today". My current query does not give me
satisfactory results, it creates a visible
delay in rendering of the main page of one of
the departments (Drugs) :8[[[
(at least I, for now, think it's the culprit).
It's for the <url: https://hyperreal.info >
site, see for yourself, notice the delay
<url: https://hyperreal.info/drugs/go.to/index >.

Currently I ask MySQL to (offending
PHP fragment follows, I hope it is self-
explanatory).

<?
$suma = 0;
$pytanie = "SELECT COUNT(DISTINCT x_article.ID) AS CNT ";
$pytanie .= "FROM x_article ";
$pytanie .= "LEFT JOIN x_instance ";
$pytanie .= "ON x_article.ID = x_instance.Article ";
$pytanie .= "LEFT JOIN x_section ";
$pytanie .= "ON x_instance.Section = x_section.ID ";
$pytanie .= "WHERE (x_section.Status & 1) = 0 "; // not empty
$pytanie .= "AND (x_section.Dept = 2 OR x_section.Dept = 5) "; // Drugs, NeuroGroove
$pytanie .= "AND (x_instance.Status & 255) = 0 "; // not hidden, etc
$pytanie .= "AND UNIX_TIMESTAMP(x_article.Date) BETWEEN " . mktime(0, 0, 1, date('m'), date('d'), date('Y')) . " AND UNIX_TIMESTAMP(NOW()) ";
$pytanie .= "GROUP BY x_article.ID";
$wynik = mysql_query($pytanie);
while ($tmp = mysql_fetch_array($wynik))
{
$suma += $tmp['CNT'];
}
if ($suma)
{
// pretty-printing of the result
$dzisdodano = str_pad((string)(int)$suma, 4, '0', STR_PAD_LEFT);
}
else $dzisdodano = '0000';
?>

The table layout is as follows:

mysql> DESC x_article;
+-------------+------------------+------+-----+----------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+----------------------+----------------+
| ID | int(10) unsigned | | PRI | NULL | auto_increment |
| Name | varchar(255) | YES | MUL | NULL | |
| Description | varchar(255) | YES | | NULL | |
| Keywords | varchar(255) | YES | | NULL | |
| Content | mediumtext | | | | |
| Date | datetime | | | 2001-01-01 00:00:00 | |
| Author | varchar(100) | | | un*****@hyperreal.pl | |
| Feedback | varchar(100) | YES | | NULL | |
| Size | int(32) | YES | | NULL | |
| Words | int(32) | YES | | NULL | |
| Images | int(32) | YES | | NULL | |
+-------------+------------------+------+-----+----------------------+----------------+

mysql> DESC x_instance;
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| Article | mediumint(9) | | MUL | 0 | |
| Section | mediumint(9) | | MUL | 0 | |
| Priority | tinyint(4) | | | 0 | |
| Status | int(16) unsigned | | | 0 | |
+----------+------------------+------+-----+---------+-------+

mysql> DESC x_section;
+----------+----------------------+------+-----+---------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------------+----------------+
| ID | mediumint(9) | | PRI | NULL | auto_increment |
| Name | varchar(100) | | MUL | | |
| Parent | mediumint(9) | | MUL | 0 | |
| Dept | smallint(6) | | MUL | 0 | |
| Priority | tinyint(4) | | | 3 | |
| Keywords | varchar(255) | YES | | NULL | |
| Sorting | varchar(255) | | | Priority DESC | |
| OrderBy | varchar(255) | YES | | NULL | |
| SplitAt | smallint(5) unsigned | | | 25 | |
| Status | int(16) unsigned | | | 0 | |
+----------+----------------------+------+-----+---------------+----------------+

Tell me if you need any additional information.
Thank you for all your help.

MySQL version is 4.0.17, PLD Linux Distribution MySQL RPM.

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007
Jul 17 '05 #1
5 2222
A few things...
1. Your AND clause for the date is making the server do way more work than
it has to. Try
$pytanie .= "AND x_article.Date BETWEEN CURDATE() AND NOW() ";
(curdate() will return the date, and will then get promoted to datetime with
the time part set to 00:00:00.)
2. Build an index on x_instance.Article and on x_article.Date (you can do
this via phpmyadmin, or you can issue the following mysql query: ALTER TABLE
x_instance ADD INDEX('Article');

That should speed it up a bit, let me know if it helps...

Adam i Agnieszka Gasiorowski FNORD wrote:
I need help width formulating the most
effective (in terms of processing time)
SQL query to count all the "new"
documents in the repository, where "new" is
defined as "from 00:00:01 up to 23:59:59
today". My current query does not give me
satisfactory results, it creates a visible
delay in rendering of the main page of one of
the departments (Drugs) :8[[[
(at least I, for now, think it's the culprit).
It's for the <url: https://hyperreal.info >
site, see for yourself, notice the delay
<url: https://hyperreal.info/drugs/go.to/index >.

Currently I ask MySQL to (offending
PHP fragment follows, I hope it is self-
explanatory).

<?
$suma = 0;
$pytanie = "SELECT COUNT(DISTINCT x_article.ID) AS CNT ";
$pytanie .= "FROM x_article ";
$pytanie .= "LEFT JOIN x_instance ";
$pytanie .= "ON x_article.ID = x_instance.Article ";
$pytanie .= "LEFT JOIN x_section ";
$pytanie .= "ON x_instance.Section = x_section.ID ";
$pytanie .= "WHERE (x_section.Status & 1) = 0 "; // not empty
$pytanie .= "AND (x_section.Dept = 2 OR x_section.Dept = 5) "; //
Drugs, NeuroGroove $pytanie .= "AND (x_instance.Status & 255) = 0 ";
// not hidden, etc $pytanie .= "AND UNIX_TIMESTAMP(x_article.Date)
BETWEEN " . mktime(0, 0, 1, date('m'), date('d'), date('Y')) . " AND
UNIX_TIMESTAMP(NOW()) "; $pytanie .= "GROUP BY x_article.ID"; $wynik
= mysql_query($pytanie);
while ($tmp = mysql_fetch_array($wynik))
{
$suma += $tmp['CNT'];
}
if ($suma)
{
// pretty-printing of the result
$dzisdodano = str_pad((string)(int)$suma, 4, '0', STR_PAD_LEFT);
}
else $dzisdodano = '0000';

The table layout is as follows:

mysql> DESC x_article;

+-------------+------------------+------+-----+----------------------+------
----------+
Field | Type | Null | Key | Default |
Extra |
+-------------+------------------+------+-----+----------------------+------
----------+ ID | int(10) unsigned | | PRI | NULL |
auto_increment |
Name | varchar(255) | YES | MUL | NULL |
|
Description | varchar(255) | YES | | NULL |
|
Keywords | varchar(255) | YES | | NULL |
|
Content | mediumtext | | | |
|
Date | datetime | | | 2001-01-01 00:00:00 |
|
Author | varchar(100) | | | un*****@hyperreal.pl |
|
Feedback | varchar(100) | YES | | NULL |
|
Size | int(32) | YES | | NULL |
|
Words | int(32) | YES | | NULL |
|
Images | int(32) | YES | | NULL |
|
+-------------+------------------+------+-----+----------------------+------
----------+
mysql> DESC x_instance;
+----------+------------------+------+-----+---------+-------+
Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+-------+
Article | mediumint(9) | | MUL | 0 | |
Section | mediumint(9) | | MUL | 0 | |
Priority | tinyint(4) | | | 0 | |
Status | int(16) unsigned | | | 0 | |

+----------+------------------+------+-----+---------+-------+

mysql> DESC x_section;

+----------+----------------------+------+-----+---------------+------------
----+ Field | Type | Null | Key | Default | Extra
|
+----------+----------------------+------+-----+---------------+------------
----+ ID | mediumint(9) | | PRI | NULL |
auto_increment |
Name | varchar(100) | | MUL | |
|
Parent | mediumint(9) | | MUL | 0 |
|
Dept | smallint(6) | | MUL | 0 |
|
Priority | tinyint(4) | | | 3 |
|
Keywords | varchar(255) | YES | | NULL |
|
Sorting | varchar(255) | | | Priority DESC |
|
OrderBy | varchar(255) | YES | | NULL |
|
SplitAt | smallint(5) unsigned | | | 25 |
|
Status | int(16) unsigned | | | 0 |
|

+----------+----------------------+------+-----+---------------+------------
----+
Tell me if you need any additional information.
Thank you for all your help.

MySQL version is 4.0.17, PLD Linux Distribution MySQL RPM.

Jul 17 '05 #2
Try breaking the query into two. First, retrieve a list of article ids that
are "new". Then in the second query, join x_instance and x_section and count
the number of unique ids that matches the additional criteria.

Użytkownik "Adam i Agnieszka Gasiorowski FNORD"
<ag*****@sieg-heil-szatan.org> napisał w wiadomo¶ci
news:3F***************@venus.ci.uw.edu.pl...

I need help width formulating the most
effective (in terms of processing time)
SQL query to count all the "new"
documents in the repository, where "new" is
defined as "from 00:00:01 up to 23:59:59
today". My current query does not give me
satisfactory results, it creates a visible
delay in rendering of the main page of one of
the departments (Drugs) :8[[[
(at least I, for now, think it's the culprit).
It's for the <url: https://hyperreal.info >
site, see for yourself, notice the delay
<url: https://hyperreal.info/drugs/go.to/index >.

Currently I ask MySQL to (offending
PHP fragment follows, I hope it is self-
explanatory).

<?
$suma = 0;
$pytanie = "SELECT COUNT(DISTINCT x_article.ID) AS CNT ";
$pytanie .= "FROM x_article ";
$pytanie .= "LEFT JOIN x_instance ";
$pytanie .= "ON x_article.ID = x_instance.Article ";
$pytanie .= "LEFT JOIN x_section ";
$pytanie .= "ON x_instance.Section = x_section.ID ";
$pytanie .= "WHERE (x_section.Status & 1) = 0 "; // not empty
$pytanie .= "AND (x_section.Dept = 2 OR x_section.Dept = 5) "; // Drugs, NeuroGroove $pytanie .= "AND (x_instance.Status & 255) = 0 "; // not hidden, etc
$pytanie .= "AND UNIX_TIMESTAMP(x_article.Date) BETWEEN " . mktime(0, 0, 1, date('m'), date('d'), date('Y')) . " AND UNIX_TIMESTAMP(NOW()) "; $pytanie .= "GROUP BY x_article.ID";
$wynik = mysql_query($pytanie);
while ($tmp = mysql_fetch_array($wynik))
{
$suma += $tmp['CNT'];
}
if ($suma)
{
// pretty-printing of the result
$dzisdodano = str_pad((string)(int)$suma, 4, '0', STR_PAD_LEFT);
}
else $dzisdodano = '0000';
?>

The table layout is as follows:

mysql> DESC x_article;
+-------------+------------------+------+-----+----------------------+------
----------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+----------------------+------
----------+ | ID | int(10) unsigned | | PRI | NULL | auto_increment | | Name | varchar(255) | YES | MUL | NULL | | | Description | varchar(255) | YES | | NULL | | | Keywords | varchar(255) | YES | | NULL | | | Content | mediumtext | | | | | | Date | datetime | | | 2001-01-01 00:00:00 | | | Author | varchar(100) | | | un*****@hyperreal.pl | | | Feedback | varchar(100) | YES | | NULL | | | Size | int(32) | YES | | NULL | | | Words | int(32) | YES | | NULL | | | Images | int(32) | YES | | NULL | | +-------------+------------------+------+-----+----------------------+------
----------+
mysql> DESC x_instance;
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| Article | mediumint(9) | | MUL | 0 | |
| Section | mediumint(9) | | MUL | 0 | |
| Priority | tinyint(4) | | | 0 | |
| Status | int(16) unsigned | | | 0 | |
+----------+------------------+------+-----+---------+-------+

mysql> DESC x_section;
+----------+----------------------+------+-----+---------------+------------
----+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------------+------------
----+ | ID | mediumint(9) | | PRI | NULL | auto_increment | | Name | varchar(100) | | MUL | | | | Parent | mediumint(9) | | MUL | 0 | | | Dept | smallint(6) | | MUL | 0 | | | Priority | tinyint(4) | | | 3 | | | Keywords | varchar(255) | YES | | NULL | | | Sorting | varchar(255) | | | Priority DESC | | | OrderBy | varchar(255) | YES | | NULL | | | SplitAt | smallint(5) unsigned | | | 25 | | | Status | int(16) unsigned | | | 0 | | +----------+----------------------+------+-----+---------------+------------
----+
Tell me if you need any additional information.
Thank you for all your help.

MySQL version is 4.0.17, PLD Linux Distribution MySQL RPM.

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007

Jul 17 '05 #3
Chung Leong wrote:
Try breaking the query into two. First, retrieve a list of article
ids that are "new". Then in the second query, join x_instance and
x_section and count the number of unique ids that matches the
additional criteria.


This should not make any improvement. Actually, it should slow it down. You
always want to use as few queries as possible. The SQL server has what's
called a "query optimizer." It looks at its attribute and relation catalogs,
and sees which conditions are the most selective (match the fewest results),
calculates the cost of various access plans etc. Given his query, and the
fact that MySQL builds an index on all primary keys, the most likely access
plan for his query would be a B+ tree traversal on x_article. This will
yield sorted data, and if an index is built on x_instance.article as I
suggested, will make for a very fast and efficient join. Breaking the query
into two would require a much more complicated selection in the second
query, and would also lose the advantage of having the articleid's
pre-sorted.
Jul 17 '05 #4
Agelmar wrote:
A few things...
1. Your AND clause for the date is making the server do way more work
than it has to. Try
$pytanie .= "AND x_article.Date BETWEEN CURDATE() AND NOW() ";
(curdate() will return the date, and will then get promoted to
datetime with the time part set to 00:00:00.)
2. Build an index on x_instance.Article and on x_article.Date (you
can do this via phpmyadmin, or you can issue the following mysql
query: ALTER TABLE x_instance ADD INDEX('Article');


hmm, just noticed that there was already an index on x_instance.Article...
just make sure that it's an index on x_instance.Article and not an index on
(x_instance.Something, x_instance.Article) (e.g. that it's the first
attribute in the index)
Jul 17 '05 #5
Agelmar wrote:
$pytanie .= "AND x_article.Date BETWEEN CURDATE() AND NOW() ";


Good idea. Thank you.

--
Seks, seksić, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info { iWanToDie } WiNoNa ) (
http://szatanowskie-ladacznice.0-700.pl foReVeR( * )
Poznaj jej zwiewne kształty... http://www.opera.com 007
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc te Vruchte | last post by:
Over the past years i've been in contact with the same problem a number of times, creating a graphical user interface on complex XML documents. Personally these solutions have never been...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
1
by: remove | last post by:
Hi all, I have a fairly simple database containing details of broadcast news reports. Each record contains information about the programme title, date, time, presenter, and details of the...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
2
by: Mejmeyster | last post by:
Hi Everyone! I've been desperately trying to prepare a database query from Access to Excel. The problem is, when I ask MS Query to import data from more than one table, no data is importing, only...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
3
by: lostdawg | last post by:
Hi, I am having trouble with the following query. I need to sort from a list of contacts the last date each was contacted. This is to be represented in days so for instance: 0-42 days...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.