数据库pro表中有2个字段:
style price
1.4-1.6*750-865 12.00
14-100*333-520 18.00
2.0-2.3*750-865 18.10
2.0-2.3*80-200 20.22
......现在需要查询price , 已经知道的条件 x1,x2x1为style里值的*前半部分范围里的值,例如x1可能为1.5也可能是20,
x2为style里值的*后半部分范围里的值,例如x2可能为350也可能是800,
已知条件:pro的字段几百万条,怎么才能做出有效率的查询出来!谢谢!
style price
1.4-1.6*750-865 12.00
14-100*333-520 18.00
2.0-2.3*750-865 18.10
2.0-2.3*80-200 20.22
......现在需要查询price , 已经知道的条件 x1,x2x1为style里值的*前半部分范围里的值,例如x1可能为1.5也可能是20,
x2为style里值的*后半部分范围里的值,例如x2可能为350也可能是800,
已知条件:pro的字段几百万条,怎么才能做出有效率的查询出来!谢谢!
style | price
1.4-1.6*750-865 | 12.00
14-100*333-520 | 18.00
2.0-2.3*750-865 | 18.10
2.0-2.3*80-200 | 20.22
......
像这种需要用范围做查询条件的最好不要扔一个字段里面去。强烈建议你重构你的表结构,
否则查询效率是个绝对的问题,需要在这里建4个复杂的函数索引,利用substr和instr来定位style中的4个值)
style 字段太复杂,每次查询,判断条件都是一个问题
* 判断规格,取得其价格
* @param list
* @param nee
*/
public BigDecimal getprice(List list,String width,String height){
PriceManageAction priman=null;
BigDecimal price=null;
String str;//存放截取*号的字符串
String strs[];//存放截取-号的字符串
float num1=0;//存放浮点型数据
float num2=0;//存放浮点型数据
for(int i=0;i<list.size();i++){
priman=(PriceManageAction)list.get(i);
//判断是否存在*
if(priman.productStyle.indexOf("*")>-1){
int start=priman.productStyle.indexOf("*");//取得*号的位置
int end=priman.productStyle.length();
str=priman.productStyle.substring(0, start);//*号前半部分
//判断存在-
if(str.indexOf("-")>-1){
strs=str.split("-");//通过-号进行分割
num1=Float.parseFloat(strs[0]);
num2=Float.parseFloat(strs[1]);
if(Float.parseFloat(height)>=num1&&Float.parseFloat(height)<=num2){
str=priman.productStyle.substring(start+1,end);//取得*号后半部分
//判断存在-
if(str.indexOf("-")>-1){
strs=str.split("-");
num1=Float.parseFloat(strs[0]);
num2=Float.parseFloat(strs[1]);
if(Float.parseFloat(width)>=num1&&Float.parseFloat(width)<=num2){
price= priman.productPrice;
}
}
}
}
}
}
return price;
}
这是最笨的办法,各位优化一下吧 只要说明理由就送分
你去sql专区问问.
兄弟,用程序过滤不是很现实~~~,几百万数据量你都放在List里面?这内存吃的小心OutOfMemory噢。。
我用sql给你弄下吧,不过效率肯定高不了,因为一定是全表扫描。
我这用的是mysql,你可以用对应的数据库的instr和substr函数来替换。
另外,由于你的出现了小数,所以我把x1的值乘了个10。如果小数点后存在多位,请自己乘10的倍数。
还有,mysql不支持函数索引,如果你的数据库支持函数索引,则可以建立x11,x12,x21,x22这4个函数索引。
代码及测试数据如下:mysql> select * from pro;
+-----------------+-------+
| style | price |
+-----------------+-------+
| 1.4-1.6*750-865 | 12.00 |
| 14-100*333-520 | 18.00 |
| 2.0-2.3*750-865 | 18.10 |
| 2.0-2.3*80-200 | 20.22 |
+-----------------+-------+
4 rows in set (0.00 sec)mysql> select * from
-> (select substr(style,1,instr(style,'-')-1)*10 as x11, -- 这里乘10
-> substr(style,instr(style,'-')+1,instr(style,'*')-1)*10 as x12, -- 这里乘10
-> substr(style,instr(style,'*')+1,instr(substr(style,instr(style,'*')+1,length(style)),'-')-1) as x21,
-> substring_index(style, '-', -1) as x22,
-> style,
-> price
-> from pro
-> )t
-> where t.x11<=20*10 -- 这里乘10
-> and t.x12>=20*10 -- 这里乘10
-> and t.x21<=350
-> and t.x22>=350;
+------+------+------+------+----------------+-------+
| x11 | x12 | x21 | x22 | style | price |
+------+------+------+------+----------------+-------+
| 140 | 1000 | 333 | 520 | 14-100*333-520 | 18.00 |
+------+------+------+------+----------------+-------+
1 row in set (0.00 sec)
需要查询price的时候用这个表来做查询而不是你原来的表,两个表的同步可以用trigger来做。其他的关系不变。
哦 诚指教 这个方法可试试看 trigger?
trigger,即数据库触发器。你在往原来的表做增、删、改的时候,同时利用触发器修改新表以达到两表数据的同步。