Gossamer Forum
Home : General : Perl Programming :

showing # of results & spanning

Quote Reply
showing # of results & spanning
I'm taking my first shot at righting my own SQL-based directory/search engine. I'm having a bit of trouble with spanning search results. I need to get the total number of results vs. the number of results shown on the page. The problem is the only way I've been able to do this is to run the same query twice: once to get the number of rows matched, and once to get results for just the current page. Any ideas?

Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com
Quote Reply
Re: showing # of results & spanning In reply to
Drew,

I really don't know if this will help, but there is a short, but sweet subroutine in DBSQL.pm (Links SQL v.1.13) that is called sub last_query. My understanding based on a reply given by Alex about using this sub is that it stores the last query in memory, which you can reference for future queries.

Regards,

Eliot Lee Wink
http://anthrotech.com/
Quote Reply
Re: showing # of results & spanning In reply to
I don't have dbman sql... but I think I found my problem. I was using rexecuting the same query and using the $dbh->rows() function, when I should have been using a count(*) in the query to get the total number of results.

Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com
Quote Reply
Re: showing # of results & spanning In reply to
Things you need to pass in to the search:
- keywords
- and/or (or you could make it more funky and support a mixture ;))
- which page
- number of results per page

With those you could execute a SQL query like:
SELECT * FROM table WHERE column LIKE '%keyword%' AND/OR column LIKE '%keyword2%' ... LIMIT m, n
where m is the beginning record ((page - 1) * number of results per page) (results start from 0).
and n is the number of results per page.

btw, Oracle doesn't support LIMIT statements, and Postgres m and n are switched around (LIMIT n, m). Can't remember how it's like with the other databases though.
That'll get ya started...

Adrian
Quote Reply
Re: showing # of results & spanning In reply to
Brewt... thanks for the reply... although the only problem I was having was the method I was using for getting the total hits. A search for "music" returned 18000 results, but took 50 seconds to load (there are about 90000 records in the database). I fixed that by using a count(*) in one query to get the total hit count, and a regular select with a limit to get the link results as links on the current page. Now the load time is an average 3.5 to 4.5 seconds on the same query. If the count query I orginally had was left out, the load times were just slightly faster than what they are now.

Here's what I'm using to generate the query:
Code:
foreach my $term (@terms) {
$clause .= "\t (";
foreach my $field (@search_fields) {
if (param('ww')) {
$clause .= " $field regexp '" . '[[:<:]]' . $term . '[[:>:]]' . "' or";
} else {
$clause .= qq| binary| if (param('cs'));
$clause .= qq| $field like '%$term%' or|;
}
}
$clause =~ s/or$//;
$clause .= ") $bool\n";
}
chomp($clause);
$clause =~ s/$bool$//;
my $search_query .= qq|
select *
from links
where
$where
order by Title
limit $offset, $max
|;
Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com
Quote Reply
Re: showing # of results & spanning In reply to
3.5 to 4.5 seconds... ouch couldn't you optimize that some more? Is the title/name field indexed? Indexing it should speed it up, since it seems the WHERE clause would probably be the thing that's taking up all the time.

Adrian
Quote Reply
Re: showing # of results & spanning In reply to
Uh...I stated LINKS SQL, NOT DBMAN SQL, Drew. Please RE-READ my REPLY!

You DO have a LINKS SQL license, don't you???

Thank you.

Regards,

Eliot Lee Wink
http://anthrotech.com/
Quote Reply
Re: showing # of results & spanning In reply to
oops. yes, you did say links sql... I didn't see that at first, just the dbsql.pm part. no I do not have a Links SQL liscense either. I don't have the money for that, which is why I'm trying to make my own.

Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com