Re^2: database sorter script
by exussum0 (Vicar) on Dec 06, 2004 at 14:37 UTC
|
Like is evil. They do full table scans since a db can't do a function index against it. If there are any databases thatcan do function indexes against "like", I'd love to know.
You are...er.. the OP would be better off, if he were using a formal DB, to create a column called indexed letter or something, that contains only the first letter, and select off of that.
----
Give me strength for today..
I will not talk it away..
Just for a moment..
It will burn through the clouds..
and shine down on me.
| [reply] |
|
|
Like is evil. They do full table scans since a db can't do a function index against it.If there are any databases that can do function indexes against "like", I'd love to know.
It depends on the database, and your query. If the query begins with a constant string (e.g. where column like 'ABC%') then the database can use an index on the column. If it begins with a wildcard (e.g. where column like '%ABC') then an index on the column can not be used. If your database determines the query plan at 'prepare' time, then it will probably not use an index on a query with placeholders such as 'where column like ?' since it doesn't know what kind of arguments you'll feed it at 'execute' time.
| [reply] [d/l] [select] |
|
|
It depends on how the indexing works. Oracle is typically smart enough for it if you use the default index scheme. But mind you, as you brought up, it's not always portable to do what you suggest. Some db's aren't savy like that.
Update in italics.
----
Give me strength for today..
I will not talk it away..
Just for a moment..
It will burn through the clouds..
and shine down on me.
| [reply] |
|
|
Re^2: database sorter script
by santander (Acolyte) on Dec 06, 2004 at 12:25 UTC
|
Sorry, its flat text database, books catalog, sort by authors data: Last Name, First Name. | [reply] |
|
|
| [reply] [d/l] [select] |
|
|
#!/usr/bin/perl -w
my $db = '/some/file';
my $letter = 'Y';
open DB, $db or die "Can't read $db: $!\n";
my @matches;
while(<DB>){
push @matches, $_ if m/^$letter/i;
}
close DB;
print sort @matches;
Or more succinctly on *nix:
$ grep -i -e ^Y db.file | sort
| [reply] [d/l] [select] |
|
|
To be more detail, database contain near 9000 records; top part contains some 'digital' names, then goes a normal alphabet. example:
666
666
Active
040 Feat Erica Baxter
16B
2 Brothers On The 4th Floor
2 Eivissa
2 Unlimited
20 Fingers
2Pac
3-O-Matic
4 Clubbers
4-2 The Floor
60 Minute Man
A Trance Communications Classic
A. Kay B.J.
A.D.A.M. Feat. Amy
A1
Aaliyah
AB Logic
ABBA
Above and Beyond
Accela
…
| [reply] |
|
|
SELECT *
FROM your_table
WHERE author_last_name LIKE 'A%'
Actually, if your flat file database is CSV (Comma Separated Value) or similar, you might be able to use Perl's DBI and DBD::CSV modules to do what you want, and you might be able to use a SELECT similar to the one above that uses the "LIKE", but I'm just speculating ;-)
HTH. | [reply] [d/l] |
|
|
To be more detail, database contain near 9000 records; top part contains some 'digital' names, then goes a normal alphabet. example:
666
666
Active
040 Feat Erica Baxter
16B
2 Brothers On The 4th Floor
2 Eivissa
2 Unlimited
20 Fingers
2Pac
3-O-Matic
4 Clubbers
4-2 The Floor
60 Minute Man
A Trance Communications Classic
A. Kay B.J.
A.D.A.M. Feat. Amy
A1
Aaliyah
AB Logic
ABBA
Above and Beyond
Accela
…
...
| [reply] |
|
|
|
|
|
|
A way to do this would be like this:
my $letter = "B";
open(my $in, "input_file") or die "Couldn't open file for read: $!";
while(<$in>) {
print if m/^$letter/;
}
You could improve it slightly by stopping once you're done processing a certain letter, but I leave that as an exercise to the reader...:)
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
| [reply] [d/l] |
|
|
If it's a flatfile database, then either convert it to SQLite or Pg or use DBD::CSV or DBD::AnyData directly on the flatfile. With those two DBDS you can use "SELECT $cols FROM $table WHERE $field LIKE 'A%' ORDER BY $field" on a flatfile. If you don't care about the case of the letter use UPPER($field), otherwise it will find only "Ant", not "ant".
| [reply] |